二进制存储的文件,多字节(例如四个字节的word)常常存在 Little-endian or Big-endian的问题,单字节的数据类型是不存在该问题的,所有的计算机字节的存储方式是完全相同的。
Little-endian or Big-endian出自《格列夫游记》,小人国的人争论从大头还是小头砸开鸡蛋。
By the way, the big-endian / little-endian naming comes from Gulliver's Travels, where the Lilliputans argue over whether to break eggs on the little-end or big-end. Sometimes computer debates are just as meaningful :-)
摘自Understanding Big and Little Endian Byte Order
Big endian即先存高位字节。
Little endian即先存低位字节。
Big endian machine: Stores data big-end first. When looking at multiple bytes, the first byte (lowest address) is the biggest.
Little endian machine: Stores data little-end first. When looking at multiple bytes, the first byte is smallest.
最明确的办法是,逐个字节的存储,这样的代码在任何机器上都不会有歧义:
c = 0; // point to location 0 (won't work on a real machine!)
*c = 0x12; // Set W's value
c = 1; // point to location 1
*c = 0x34; // Set X's value
... // repeat for Y and Z; details left to reader