Struct usage and varying sizes of h, l, etc

Ethan Furman efurman at admailinc.com
Wed May 21 14:42:00 EDT 2008


John Machin wrote:

> Robert Kern wrote:
>
>> Ethan Furman wrote:
>>
>>> Greetings,
>>>
>>> I'm looking at the struct module for binary packing of ints and 
>>> floats.  The documentation refers to C datatypes.  It's been many 
>>> years since I looked at C, but I seem to remember that the data type 
>>> sizes were not fixed -- for example, an int might be two byes on one 
>>> machine, and four bytes on the next.  Can any C programmers verify 
>>> this?  If it is true, does that mean that struct.pack('h', 8001) 
>>> might give me different results depending on the machine it's 
>>> running on?
>>
>>
>> Right. I believe (but could be wrong) that "char" is defined to be 
>> one byte, but that "short", "int", "long", and "long long" are 
>> defined as "at least as big as the previous type".
>>
>> In practice, though, on nearly every machine that Python runs on, 
>> "char" is one byte, "short" is two bytes, and "int" is four bytes. 
>> "longs" and "long longs" tend to vary substantially, though; never 
>> assume sizes for them.
>>
>> Single-precision floats are always four bytes and double-precision 
>> floats are always eight bytes. "long doubles" vary; they could be 
>> twelve bytes or sixteen.
>>
>> If you want to deal with fixed sizes, use struct.calcsize() to test 
>> the sizes of each of the integer types, assign them to width-specific 
>> aliases, and always use these aliases.
>>
>
> This is all true if you want to operate in "native" mode; however in 
> "standard" mode the sizes are fixed -- otherwise there'd be no easy 
> way of reading/writing the fixed-size fields in many common file formats.
>
> As the manual says:
> """
> Native size and alignment are determined using the C compiler's sizeof 
> expression. This is always combined with native byte order.
>
> Standard size and alignment are as follows: no alignment is required 
> for any type (so you have to use pad bytes); short is 2 bytes; int and 
> long are 4 bytes; long long (__int64 on Windows) is 8 bytes; float and 
> double are 32-bit and 64-bit IEEE floating point numbers, respectively.
> """
>
> If, as I suspect, Ethan's purpose is be able to read/write files in a 
> long-established PC file format, he will need to '<' for littleendian 
> order, and an appropriate selection from bBhHiI and d will do what he 
> needs.
>
> HTH,
> John
> -- 
> http://mail.python.org/mailman/listinfo/python-list

John, you are correct in my purpose, and thank you (and to everyone) for 
the clarification.  It does indeed help a great deal.
--
Ethan




More information about the Python-list mailing list