original articles, reproduced please specify the source https://blog.csdn.net/aaron_lyn1985/article/details/54669451
One, byte order
The byte order is the result of different host processors and operating systems, and the order in which variables larger than one byte are stored in memory.
The byte order usually has two kinds of classification methods (Big Endian) and small End byte order (Little Endian).
second, big, small end
"Big-endian" and "small End" indicate which end of a multibyte value is stored at the starting address of the value, where the small end is stored at the start address, which is the small-endian byte-order, and the big end is stored at the starting address, which is the endian byte;
1. The small-end method (Little-endian) is the low-bit bytes emitted in the memory of the lower address end (that is, the starting address of the value), high-byte emissions in the memory of the higher address;
2. Big-endian (Big-endian) is the high-bit byte emitted at the low address end of the memory (that is, the starting address of the value), low-bit bytes emitted in the memory of the higher address;
Example 1: For the integer data 0x12345678, it is in the big-endian and small-end method of the system, the respective storage method as shown in Figure 1:
Example 2:
Big-endian sequence:
The data is in 8bit:
In the example, the most significant bit (MSB, most significant Byte) is 0x0a stored at the lowest memory address. The next byte 0x0b exists at the address at the back. is similar to the hexadecimal byte reading order from left to right.
Small End Order:
The data is in 8bit:
Address growth Direction → |
... |
0x0D |
0x0C |
0x0B |
0x0A |
... |
The least significant bit (Lsb,least significant Byte) is 0x0d stored at the lowest memory address. The subsequent bytes exist sequentially at the address at the back.
The data is in 16bit:
Address growth Direction → |
... |
0x0c0d |
0x0a0b |
... |
The lowest 16bit unit 0x0c0d is stored at low levels.
third, network byte order
Network byte order is a well-defined data representation format in TCP/IP, which is independent of the specific CPU type, operating system and so on, so that the data can be interpreted correctly when transferring between different hosts.
The data transmitted on the network is a byte stream, and for a multi-byte numeric value, which bytes are passed first when the network is transmitted. That is, when the receiving end receives the first byte, it handles the byte as a high byte or a low byte.
The TCP/IP protocol specifies that the first byte received is treated as a high byte, which requires that the first byte sent by the sending side is a high byte, whereas the first byte sent when the sender sends the data is the byte that corresponds to the value at the start address in memory, that is, The byte that corresponds at the start address in memory is the first high-order byte to be sent (that is, the high byte is stored at the low address), so that the multibyte value is stored in memory as the big-endian method before it is sent.
So, the network byte order is the big-endian byte order.
Example: When we send integer numeric 0x12345678 through the network, in the x86 platform, it is stored in the small end, before sending, we need to use the system-provided byte-order conversion function htonl () to convert it into the value of big-endian storage, as shown in Figure 2 below:
The Berkeley BSD Socket API defines a set of conversion functions for converting 16 and 32bit integers between the network order and the native byte order:
Htonl,htons is used for the conversion of the sequence to the network sequence; The Ntohl,ntohs is used to convert the network sequence to the native order.
#define NTOHS (n)//network byte order to host byte order n represents NET, H stands for Host, S represents short
#define HTONS (n)//host byte order to network byte order n represents NET, H stands for 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 represents NET, H stands for host, L stands for long
These functions are defined as empty macros in systems that use the big endian type.
In the development of network programs, or cross-platform development, should be careful to ensure that only one byte order, or the two sides of the interpretation of the same will produce a bug.
Four, byte sequence test
The byte order on different CPU platforms is usually not the same, and the following simple code can test the byte order on different platforms:
#include
#include //If Windows
//#pragma comment (lib, "Ws2_32.lib") if Windows
int main (int Argc,char * * argv)
{
int num = 0x12345678;
unsigned char* pc = (unsigned char*) (&num);
printf ("local order:\n");
printf ("[0]: 0x%X addr:%u\n", pc[0], &pc[0]);
printf ("[1]: 0x%X addr:%u\n", pc[1], &pc[1]);
printf ("[2]: 0x%X addr:%u\n", pc[2], &pc[2]);
printf ("[3]: 0x%X addr:%u\n", pc[3], &pc[3]);
num = htonl (num);
printf ("htonl order:\n");
printf ("[0]: 0x%X addr:%u\n", pc[0], &pc[0]);
printf ("[1]: 0x%X addr:%u\n", pc[1], &pc[1]);
printf ("[2]: 0x%X addr:%u\n", pc[2], &pc[2]);
printf ("[3]: 0x%X addr:%u\n", pc[3], &pc[3]);
return 0;
}
Output on the SPARC platform:
Local order:
[0]: 0x12 addr:4290770212//high-bit byte stored at the low address, it is the big-endian method;
[1]: 0x34 addr:4290770213
[2]: 0x56 addr:4290770214
[3]: 0x78 addr:4290770215//Low byte stored at the high address;
HTONL Order:
[0]: 0x12 addr:4290770212//This shows that the host byte sequence is the same as the network byte;
[1]: 0x34 addr:4290770213
[2]: 0x56 addr:4290770214
[3]: 0x78 addr:4290770215
Output on the X86 platform:
Local order:
[0]: 0x78 addr:4289157020//low-bit bytes stored at the low address, it is the small-end method;
[1]: 0x56 addr:4289157021
[2]: 0x34 addr:4289157022
[3]: 0x12 addr:4289157023//high-bit byte is stored at the higher address;
HTONL Order:
[0]: 0x12 addr:4289157020//This shows that the host byte sequence is not the same as the network byte;
[1]: 0x34 addr:4289157021
[2]: 0x56 addr:4289157022
[3]: 0x78 addr:4289157023