Unpacking problem

C

Chris Garland

What's wrong here?

I can unpack an unsigned char(144,)

I can unpack a short(6,)

But an unsigned char & a short give me thisTraceback (most recent call last):
 
M

Marc 'BlackJack' Rintsch

Chris Garland said:
But an unsigned char & a short give me this
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
 
T

timm.gloger

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

Actually calculating the size for 'hB' results in: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(144,6)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top