Basic byte order
For a single byte (A byte), Most processors process bits in the same order, so the storage method of a single byte is generally the same as the transmission method.
For multi-byte data, such as integers (4 bytes in 32-bit machines), there are two main storage methods for different processors. The storage method of 0x0a0b0c0d in memory is used as an example, the following methods are available:
-
Note:0xThe prefix indicates the hexadecimal format.
Big client Sequence
The big-Endian sequence is also called the Big-Endian sequence.
Data in 8 bits
In the example, the highest valid bit (MSB, most significant byte) is0x0aStored at the lowest memory address. Next byte0x0bThere is a later address. It is similar to the reading order of hexadecimal bytes from left to right.
Small client order
The smallest Order (English: Little-Endian) or the smallest tail order.
Address growth direction → |
... |
0x0d |
0x0c |
0x0b |
0x0a |
... |
The minimum valid bit (LSB, least significant byte) is0x0dStored at the lowest memory address. The next byte is followed by the address.
Address growth direction → |
... |
0x0c0d |
0x0a0b |
... |
Minimum 16-bit unit0x0c0dLow storage.
- Change the address growth direction:
When you change the address growth direction from right to left, the table is more readable.
Growth direction of the ingress address |
... |
0x0a |
0x0b |
0x0c |
0x0d |
... |
The minimum valid bits (LSB) is0x0dStored at the lowest memory address. The next byte is followed by the address.
Growth direction of the ingress address |
... |
0x0a0b |
0x0c0d |
... |
Minimum 16-bit unit0x0c0dLow storage.
Method for judging the big end and small end: # Include <stdio. h> </P> <p> int main (INT argc, char * argv []) <br/> {<br/> int I = 1; <br/> char * P = (char *) & I; <br/> If (* P = 1) <br/>{< br/> printf ("small-end mode "); <br/>}< br/> else <br/> {<br/> printf ("big end mode "); <br/>}</P> <p> return 0; <br/>}</P> <p>Char is a byte. Therefore, to forcibly point the char type P to I, P must point to the lowest address of I. Then, you can determine whether the value in P is 1 to determine whether it is a small end.
Network order
Network Transmission generally adopts the big-End sequence, which is also calledNetwork byte order, OrNetwork order. In the IP protocol, the large client order is defined as the network byte order.
Berkeley Socket API defines a set of conversion functions for 16 and 32-bit integers in the network order andLocal byte order. Htonl and htons are used for local sequence conversion to network sequence; ntohl and ntohs are used for network sequence conversion to local sequence.
# Define ntohs (n) // network byte order to host byte order n Represents net, H represents host, s represents short # define htons (N) // host byte order to network byte order n Represents net, H represents host, s represents short # define ntohl (n) // network byte order to host byte order n Represents net, H stands for host, L stands for long # define htonl (n) // host byte order to network byte order N stands for net, H stands for host, L stands for long references: [1] bytes