Unpack Expects the Wrong Number of Bytes

John Machin sjmachin at lexicon.net
Sat Jul 18 10:47:15 EDT 2009


On Jul 19, 12:34 am, Timothy Crone <tjcr... at gmail.com> wrote:
> Hello,
>
> I have noticed that for certain format strings, struct.unpack expects
> the wrong number of bytes.
[snip]
> header = "si"
> data = list(unpack(header,f.read(5)))
>
> throws
>
> struct.error: unpack requires a string argument of length 8
>
> So unpack expects 7 additional bytes when an integer is added to the
> format string. Does anyone know what is going on?

Alignment. 1 byte for the "s", 3 pad bytes to get to the next "i"
boundary, 4 bytes for the "i".

http://docs.python.org/library/struct.html
Then Ctrl-F search for "align"

Choose a prefix character whose MO matches the struct that you need to
work with.

Cheers,
John



More information about the Python-list mailing list