I am using Python to convert some files to a binary format, but I've run into an odd snare.
Problem
Code
import struct
s = struct.Struct('Bffffff')
print s.size
Result
28
Obviously the expected size would be 25, but it appears to be interpreting the first byte (B) as a 4-byte integer of some kind. It will also write out a 4-byte integer instead of a byte.
Work-around
A work-around exists, namely separating the B out into a separate struct, like so:
Code
import struct
s1 = struct.Struct('B')
s2 = struct.Struct('ffffff')
print s1.size + s2.size
Result
25
Is there any explanation for this behavior?
print struct.Struct('ffffffB').sizeprints 25.