BloodSexMagik BloodSexMagik - 9 days ago 4
Python Question

Python struct format character sizes, why is it bigger than it should be?

Simple question if do the following:

import struct
struct.calcsize("6cHcBHIIQ")


returns 32 when I believe it should be 28.

By doing the following (missing the Q):

import struct
struct.calcsize("6cHcBHII")


it returns 20, which is what I would expect.

and doing:

import struct
struct.calcsize("Q")


returns 8, which is correct.

Why does adding the Q onto the top one result in 12 extra bytes being expected instead of 8?

Python 3, windows machine.

Thanks.

Answer

Alignment. See https://docs.python.org/3/library/struct.html#struct-alignment for more details.

Try struct.calcsize("=6cHcBHIIQ").

Comments