pythonstruct

Why does the size of a python struct depend on the endianess?


Why does the size of a struct change if endianness is specified, notably even when the endianness matches the native endianness of the platform?

Example:

>>> import struct
>>> struct.calcsize("BI")
8
>>> struct.calcsize(">BI")
5
>>> struct.calcsize("<BI")
5
>>> 

Why is the extra padding added?


Solution

  • If byte ordering is not specified then it's implicitly "@" in which case both the size and alignment are native. See the "Format Strings" section of this document. You will note that "@" is the only format string prefix that forces alignment. The format character "I" denotes an unsigned integer which has a size of 32 bits. The format character "B" denotes an unsigned char (8 bits). Thus, the integer will be aligned on a 32-bit boundary which leads to the padding. When alignment does not occur then the packed bytes object will just be the size of the sum of its constituent parts which, in this case will be 1+4==5