endianness theory and concept

2019-05-25 04:09发布

This isn't a question specific to any programming language. Say you have some file written on a big-endian machine, and you know this. If two single-byte values were written back-to-back, how would you know? Big-endian reverses the order of 16, 32, and 64 bit values, so how would you know you need to read it as individual bytes?

For instance, you write the byte 0x11, then the byte 0x22. The file then contains 0x1122. If you read that on a little endian machine, you'd have to convert it. So would you read it as 2211, or 1122? Would you know how?

Does this make any sense? I feel like I'm missing something super basic here.

标签: endianness
8条回答
干净又极端
2楼-- · 2019-05-25 04:33

You need to either divine it because you know something else (i.e., you know you are reading a file in big endian format) or you need to encode the endianness in the file somehow. Unicode text files use 0xFFFE (or something similar) as the first two bytes of a text file to calculate endianness. If you read it as 0xfffe, then it's in the native endian format. If you read it as 0xfeff, it's not.

查看更多
祖国的老花朵
3楼-- · 2019-05-25 04:35

The processor operates in one or the other endian mode (some can switch based on pages, etc). They don't know if they're doing the right thing or not. They just do what they do. (Garbage In, Garbage Out) :-)

查看更多
登录 后发表回答