struct size confusion:
m.yanowitz at kearfott.com
Wed Mar 22 16:26:04 CET 2006
Thanks for your and everyone else's feedback.
I got it to work now by prefixing the PACK_FORMAT with "!".
I previously thought I could only use the "!' with the unpack.
I still don't fully understand the byte allignment stuff (I am
sure I will get it eventually), but I am content that it is
From: python-list-bounces+m.yanowitz=kearfott.com at python.org
[mailto:python-list-bounces+m.yanowitz=kearfott.com at python.org]On Behalf
Of Fredrik Lundh
Sent: Wednesday, March 22, 2006 9:28 AM
To: python-list at python.org
Subject: Re: struct size confusion:
Michael Yanowitz wrote:
> I am relatively new to Python and this is my first post on
> this mailing list.
> I am confused as to why I am getting size differences in the following
> >>> print struct.calcsize("I")
> >>> print struct.calcsize("H")
> >>> print struct.calcsize("HI")
> >>> print struct.calcsize("IH")
> Why is it 8 bytes in the third case and why would it be only 6 bytes
> in the last case if it is 8 in the previous?
because modern platforms tend to use an alignment equal to the size of
the item; 2-byte objects are stored at even addresses, 4-byte objects
are stored at addresses that are multiples of four, etc.
in other words, HI is stored as 2 bytes H data plus 2 bytes padding plus
four bytes I data, while IH is four bytes I data, no padding, and 2 bytes
> I tried specifying big endian and little endian and they both have
> the same results.
are you sure? (see below)
> I suspect, there is some kind of padding involved, but it does not
> seem to be done consistently or in a recognizable method.
the alignment options are described in the library reference:
default is native byte order, native padding:
to specify other byte orders, use a prefix character. this also disables
More information about the Python-list