struct unpack issue

Miles semanticist at gmail.com
Sat Jun 14 01:15:52 EDT 2008


On Fri, Jun 13, 2008 at 9:27 PM, Ping Zhao <ioceanio at gmail.com> wrote:
> However, when I tried to rewrite them in short:
>
> header = struct.unpack('2s1i', self.__read(src, 6))
> ...
> It was weired that the required argument length increased to 8.

This is an alignment issue; if you don't specify a structure format
character, the struct module acts like the data is laid out in memory
as a C compiler would do it.

http://en.wikipedia.org/wiki/Data_structure_alignment
http://docs.python.org/lib/module-struct.html

Specifying little-endian byte order will force the data to be packed
(unaligned), and will also make your code more portable:

struct.unpack('<2s1i', ...)

-Miles



More information about the Python-list mailing list