how many bytes in an int
"Martin v. Löwis"
martin at v.loewis.de
Mon Aug 9 02:43:18 EDT 2004
Reid Nichol wrote:
> I'm thinking of writing a movie file encoder (probably avi). So, I need
> to output DWORD (lookup revealed its a 4-byte int) to a binary file.
> Therefore I need to know whether this can be done in python or not,
> which will tell me whether I'll try to do it or not.
You looked up DWORD somewhat incorrectly. It is a four byte int in
memory, but on disk, it is a little-endian byte string of four bytes.
So you *do* need the struct module, because only that will give you
byte strings (of course, Grant's formula also works)
> But, since the 64-bit archecture is out, short, long, etc may change
> there meanings quite soon. From what I've read in the struct module
> docs I can only tell it that it's a short, long, etc. but not whether
> it's exactly a 4-byte int. Is there a way to do this?
As Grant says: use the struct module. Use struct.calcsize to find out
how large an int is. If the size is too large, try a short. If the size
is too small, try a long. If no type matches, take the next larger type,
and drop the extra bytes.
However, it does not actually need to be that difficult: "int" is 32-bit
on all current systems, including all 64-bit systems (only long is
64-bits on some 64-bit systems).
Regards,
Martin
More information about the Python-list
mailing list