on the definition of byte-order (big-endian, small-end method)
Unxi Network Programming Definition: The term "small end" and "big endian" denote which end of a multibyte value (small or big) is stored at the starting address of the value. The small end has the starting address, that is, the small-endian byte-order;
It can also be said that:
1. The small-end method (Little-endian) is the low-level byte emissions at the lower address of the memory is the starting address of the value, high-bit bytes emitted in the memory of the higher address.
2. Big-endian (Big-endian) is the high-bit byte emission at the low address end of the memory that is the starting address of the value, the low byte is emitted at the upper address end of memory.
For a simple example, for plastic 0x12345678. It is stored in the system of the big-endian and the small-end method, respectively, 1 of the way.
Network byte order
We know that the traffic on the network is a byte stream, and for a multi-byte value, which bytes are passed first when the network is transmitted? In other words, when the receiver receives the first byte, does it handle the byte as a high or low?
Network byte order definition: The first byte received is treated as a high level, which requires that the first byte sent by the sending side should be high. When sending data on the sending side, the first byte sent is the byte that corresponds to the number in the in-memory start address. Visible multibyte values before sending, in-memory values should be stored in the big-endian method.
The network byte order is said to be the big endian byte sequence.
For example, we go through the network to send 0x12345678 this shaping, in the 80x86 platform, it is stored in small-end method, before the transmission needs to use the system provided by the HTONL to convert it into big-endian storage, 2 shows.
byte order test program
The byte order on different CPU platforms is usually not the same, write a simple C program below, which can test the byte order on different platforms.
1 |
#include <stdio.h> |
2 |
#include <netinet/in.h> |
3 |
int main () |
4 |
{ |
5 |
int i_num = 0x12345678; |
6 |
printf ("[0]:0x%x\n", * ((char *) &i_num + 0)); |
7 |
printf ("[1]:0x%x\n", * ((char *) &i_num + 1)); |
8 |
printf ("[2]:0x%x\n", * ((char *) &i_num + 2)); |
9 |
printf ("[3]:0x%x\n", * ((char *) &i_num + 3)); |
10 |
|
11 |
I_num = htonl (i_num); |
12 |
printf ("[0]:0x%x\n", * ((char *) &i_num + 0)); |
13 |
printf ("[1]:0x%x\n", * ((char *) &i_num + 1)); |
14 |
printf ("[2]:0x%x\n", * ((char *) &i_num + 2)); |
15 |
printf ("[3]:0x%x\n", * ((char *) &i_num + 3)); |
16 |
|
17 |
return 0; |
18 |
} |
On the 80X86CPU platform, the execution of the program results in the following:
[0]:0x78
[1]:0x56
[2]:0x34
[3]:0x12
[0]:0x12
[1]:0x34
[2]:0x56
[3]:0x78
Analysis results, on the 80x86 platform, the system stores the low-level of the multi-byte in the variable start address, using the small-end method. HTONL converts i_num into network byte order, and visible network byte order is the big-endian method.
Summary point: 80x86 uses the small end method, the network byte order uses the big-endian method.
On the definition of byte-order (big-endian, small-end method)