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
-
41. "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." 2. Because of padding.jonrsharpe– jonrsharpe2017年09月05日 06:22:51 +00:00Commented Sep 5, 2017 at 6:22
-
Understood. I should have paid more attention to documentation. @jonrsharpe please add this as answer. I would like to accept this answer.shrishinde– shrishinde2017年09月05日 06:34:02 +00:00Commented Sep 5, 2017 at 6:34
1 Answer 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