问题
Couple of issues with python struct. Please let me know what is correct.
Document mentions length of l/L as 4 but when checked with calcsize it gives 8.
>>> struct.calcsize('l') 8struct module calcsize is giving wrong size. If individual element size is calculated, it's sum is 90 but when calculated together with calcsize it gives 92.
>>> struct.calcsize('8s2sIII30s32s6s') 92 >>> struct.calcsize('8s') 8 >>> struct.calcsize('2s') 2 >>> struct.calcsize('III') 12 >>> struct.calcsize('30s') 30 >>> struct.calcsize('32s') 32 >>> struct.calcsize('6s') 6
回答1:
Elaborating answer posted by jonrsharpe in comments.
The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent.
>>> struct.calcsize('l') 8 >>> struct.calcsize('=l') 4Because of padding. Use = to not use padding.
>>> struct.calcsize('=8s2sIII30s32s6s') 90
来源:https://stackoverflow.com/questions/46048166/python-struct-giving-incorrect-length