Based on the big end method small end method and network byte order in depth understanding
- 2020-04-02 00:55:36
- OfStack
You can also say:
1. Little-endian refers to the low address end of memory where the low byte is emitted, that is, the starting address of the value, while the high byte is emitted at the high address end of memory.
2. Big-endian method refers to the high-byte emission at the low-address end of memory, namely the starting address of the value, and the low-byte emission at the high-address end of memory.
As a simple example, for the reconstructive 0x12345678. It is stored in the system of big-end method and small-end method, as shown in figure 1.
< img style = "border = 0 border - RIGHT - WIDTH: 0 px; DISPLAY: inline. BORDER - TOP - WIDTH: 0 px; BORDER - BOTTOM - WIDTH: 0 px; BORDER - LEFT - WIDTH: 0 px "title = zijiexu_pic_1 BORDER = 0 Alt = zijiexu_pic_1 SRC =" / / files.jb51.net/file_images/article/201305/201305271703152.jpg "WIDTH = 603 height = 378 >
We know that the data flow on the network is a byte stream, for a multi-byte value, when the network transmission, which byte is passed first? That is, when the receiver receives the first byte, does it process the byte as a high or low byte?
Network byte order definition: the first byte received is treated as a high byte, which requires that the first byte sent by the sender should be a high byte. When the sender sends data, the first byte sent is the byte corresponding to the starting address of the number in memory. Note that multi-byte values should be stored in memory in the big-end method before they are sent.
Network byte order is called big-endian byte order.
For example, we send 0x12345678 through the network, which is stored in the small-end method in the 80X86 platform. Before sending, we need to use the htonl provided by the system to convert it to the big-end method, as shown in figure 2.
< img style = "border = 0 border - RIGHT - WIDTH: 0 px; DISPLAY: inline. BORDER - TOP - WIDTH: 0 px; BORDER - BOTTOM - WIDTH: 0 px; BORDER - LEFT - WIDTH: 0 px "title = zijiexu_pic_2 BORDER = 0 Alt = zijiexu_pic_2 SRC =" / / files.jb51.net/file_images/article/201305/201305271703153.jpg "WIDTH = 605 height = 378 >
Byte order test program
Byte order is usually different on different CPU platforms, so let's write a simple C program that can test byte order on different platforms.
#include <stdio.h>
#include <netinet/in.h>
int main()
{
int i_num = 0x12345678;
printf("[0]:0x%xn", *((char *)&i_num + 0));
printf("[1]:0x%xn", *((char *)&i_num + 1));
printf("[2]:0x%xn", *((char *)&i_num + 2));
printf("[3]:0x%xn", *((char *)&i_num + 3));
i_num = htonl(i_num);
printf("[0]:0x%xn", *((char *)&i_num + 0));
printf("[1]:0x%xn", *((char *)&i_num + 1));
printf("[2]:0x%xn", *((char *)&i_num + 2));
printf("[3]:0x%xn", *((char *)&i_num + 3));
return 0;
}
On the 80X86CPU platform, the following results are obtained by executing this program:
[0] : 0 x78
[1] : 0 x56
[2] : 0 x34
[3] : 0 x12
[0] : 0 x12
[1] : 0 x34
[2] : 0 x56
[3] : 0 x78
As a result, on the 80X86 platform, the system stores the low bits in multiple bytes in the variable's starting address, using the small-end method. Htonl converts i_num to network byte order, so network byte order is the big-end method.
Summary: 80X86 USES the small-end method, and network byte order USES the big-end method.