Unpacking problem

timm.gloger at gmail.com timm.gloger at gmail.com
Thu Mar 1 17:29:48 EST 2007


The problem is, that len('\x90\x06\x00') is not equivalent to
calcsize('Bh'):
>>> calcsize('Bh')
4
>>> len('\x90\x06\x00')
3

Actually calculating the size for 'hB' results in:
>>> calcsize('hB')
3

So far I have not figured out, why there is an additional byte, but it
does not effect the result in any way. (Or I simply did not insert any
byte that would cause an effect.)

So
>>> unpack('Bh', '\x90\x??\x06\x00')
(144,6)

with ?? as any valid hex, gives the "correct" result, but I dont know
why either.

On Mar 1, 10:17 pm, "Chris Garland" <chrisgarlan... at gmail.com> wrote:
> What's wrong here?
>
> >>> from struct import unpack
>
> I can unpack an unsigned char>>> unpack('B','\x90')
>
> (144,)
>
> I can unpack a short>>> unpack('h','\x06\x00')
>
> (6,)
>
> 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
>
>





More information about the Python-list mailing list