Large-end, small-end, and network-byte Conversion

Source: Internet
Author: User
Definition of the big-end method, small-end method, and network-byte sequence (large-end method, small-end method)

Unxi network programming defines the terms "small-end" and "Big-end", which indicate which end of the Multi-byte value (small-end or large-end) is stored at the starting address of the value. There is a starting address for a small end, that is, the byte order for a small end, and the starting address for a large end, that is, the byte order for a large end.

 

You can also say:
1. The little-Endian method is the starting address of the Low-byte emission in the memory, that is, the low address of the value. The high byte is discharged to the high address of the memory.
2. The big-Endian (big-Endian) method is the low address of the high byte emission in the memory, that is, the starting address of this value. The low byte is discharged to the high address of the memory.

For a simple example, the integer is 0x12345678. It is stored in the system of the big-end method and the Small-end method, as shown in 1.

 

 

Network byte order

We know that the data stream on the network is a byte stream. For a multi-byte value, which byte is transmitted during network transmission? That is to say, when the receiving end receives the first byte, does it process this Byte as a high or a low byte?

Network byte order definition: the first byte received is regarded as a high level, which requires that the first byte sent by the sending end be a high level. When the sender sends data, the first byte sent is the byte corresponding to the starting address of the number in the memory. It can be seen that before sending the multi-byte value, the value in the memory should be stored in the upper-end method.

The Network byte sequence is the large-end byte sequence.
For example, if the integer 0x12345678 is sent over the network, it is stored as a small terminal on the 80x86 platform. before sending the integer, you need to use the htonl provided by the system to convert it to a large terminal for storage, 2.

 

Byte sequence test program

The byte order on different CPU platforms is usually different. Below is a simple C program 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, execute this program and obtain the following results:
[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 low-byte values in the Variable Start address, using the small-end method. Htonl converts I _num to the network byte order. It can be seen that the network byte order is the big-end method.

Conclusion: 80x86 uses the small-end method and the large-end Method for network byte order.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.