I’m attempting to write a Python C extension that reads packed binary data (it is stored as structs of structs) and then parses it out into Python objects. Everything works as expected on a 32 bit machine (the binary files are always written on 32bit architecture), but not on a 64 bit box. Is there a ‘preferred’ way of doing this?
It would be a lot of code to post but as an example:
struct { WORD version; BOOL upgrade; time_t time1; time_t time2; } apparms; File *fp; fp = fopen(filePath, 'r+b'); fread(&apparms, sizeof(apparms), 1, fp); return Py_BuildValue('{s:i,s:l,s:l}', 'sysVersion',apparms.version, 'powerFailTime', apparms.time1, 'normKitExpDate', apparms.time2 );
Now on a 32 bit system this works great, but on a 64 bit my time_t sizes are different (32bit vs 64 bit longs).
Damn, you people are fast.
Patrick, I originally started using the struct package but found it just way to slow for my needs. Plus I was looking for an excuse to write a Python Extension.
I know this is a stupid question but what types do I need to watch out for?
Thanks.
Explicitly specify that your data types (e.g. integers) are 32-bit. Otherwise if you have two integers next to each other when you read them they will be read as one 64-bit integer.
When you are dealing with cross-platform issues, the two main things to watch out for are:
0x0000000C) will be read as 201326592 (0x0C000000).Hopefully this helps.