About Endianness
大小端问题(big endian little endian)
In computing, endianness refers to the order of bytes (or sometimes bits) within a binary representation of a number
In its most common usage, endianness indicates the ordering of bytes within a multi-byte number. A big-endian ordering places the most significant byte first and the least significant byte last,
while a little-endian ordering does the opposite
Historically, various methods of endianness have been used in computing, including exotic forms such as middle-endianness. Today, however, big-endianness is the dominant ordering in networking protocols (IP, TCP, UDP). Conversely, little-endianness is the dominant ordering for processor architectures (x86, most ARM implementations) and their associated memory. File formats can use either ordering; some formats use a mixture of both.
大小端的定义
Big Endian Byte Order:
The most significant byte (the “big end”) of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the next three bytes in memory.
Little Endian Byte Order: (LL)
The least significant byte (the “little end”) of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the next three bytes in memory.
linux 中转换大小端的函数
1 | $ man endian |
通过以上命令可以获取到转换大小端的函数
1 | // h short for host |
大小端的影响范围
- 文件系统的创建
- 网络传输协议的字节序(一般是大端,也有小端Server_Message_Block)
判断大小端的C代码
1 | // from linux-5.4.3\arch\arm\kernel\setup.c 中161行的一个宏, |