Binary Files on 32bit / 64bit systems?
I am using python
struct module to create custom binary files.
The file itself has the following format:
4 bytes
(integer)
1 byte
(unsigned char)
4 bytes
(float)
4 bytes
(integer)
1 byte
(unsigned char)
4 bytes
(float)
.......................... (100000 such lines)
4 bytes
(integer)
1 byte
(unsigned char)
4 bytes
(float)
Currently, I am using a 32bit machine to create these custom binary files. I am soon planning on switching to a 64bit machine.
Will I be able to read开发者_StackOverflow中文版/write the same files using both {32bit / 64bit}
machines? or should I expect compatibility issues?
(I will be using Ubuntu Linux for both)
As long as your struct format string uses "standard size and alignment" (<
or >
) rather than "native size and alignment" (@
), your files can be used cross-platform.
See http://docs.python.org/library/struct.html#byte-order-size-and-alignment
By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).
So it depends on your code if it's portable or not.
You have more to worry about than 32-bit vs. 64-bit. The broad category you are talking about is called serialization.
Have a look at the marshal and/or pickle modules.
精彩评论