Unpacking problem

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Mar 1 23:11:26 CET 2007


In <1172783865.479377.42790 at h3g2000cwc.googlegroups.com>, Chris Garland
wrote:

> But an unsigned char & a short give me this
>>>> unpack('Bh','\x90\x06\x00')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> struct.error: unpack str size does not match format

Let's pack this:

In [90]: pack('Bh', 0x90, 0x6)
Out[90]: '\x90\x00\x06\x00'

Per default the values are (un)packed with the preferred alignment of the
C compiler, so here is an extra byte to place the short at an even
address.  If you give the endianess in the format string there's no
padding:

In [91]: pack('<Bh', 0x90, 0x6)
Out[91]: '\x90\x06\x00'

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list