Byte sequence conversion in Linux

Source: Internet
Author: User
If you want to write a network program in Linux, you will encounter this bytecode problem,

Instead of searching for materials every time you encounter it, you should make a simple diagram.

All the information mentioned here is verified on your own machine, but it is also a personal understanding,

Errors are inevitable.

The byte order refers to how the computer stores the data level when arranging data storage. In fact, this

Generally, it has nothing to do with the operating system, but is related to the hardware architecture of the computer. Specifically, it is the CPU design.

The intel chip and AMD chipset are both in the small-end byte sequence (the lowest byte priority storage ),

However, some special CPUs are not handled in this way. They may use the large-end byte sequence (the highest byte priority storage ),

These byte sequences used on different computers are called host byte sequences.

In addition, it should be clear that the most basic data storage supported by the computer CPU is a byte, and the data bit operation is used

A special register. Therefore, the data type involved in the bytecode issue must be a multiple of the entire byte, not 4 bits.

It is not a single byte. in hexadecimal notation, It is a data 0xab, which cannot be converted to 0xba in byte order.

In order to communicate with computers in different architectures, the byte order on the network must be unified.

It uses the large-end byte order (the highest byte priority storage), so we usually want to do the byte order conversion in the program,

In order to facilitate program transplantation, the bytes must be converted in sequence when such operations are involved. Conversion of byte order

In other words, when the program runs dynamically on the system, the conversion or

No conversion, so it is necessary to use the byte sequence conversion function, which makes it easy for our program to be transplanted.

Detailed processing functions and related data changes are provided:

The figure shows the common terms in various materials. They all refer to the same thing! For how data is stored on the network, the author understands that there may be different bytes in the gateway or other network devices for data filtering and processing, however, the data arriving at our network adapter must be stored in byte order. When the NIC receives a data packet, it applies for a memory area and then stores the data, at this time, data is stored in byte order of the network. When our software process needs to read the data here, the CPU will find the address and read a certain number of bytes according to the instruction, at this time, if we do not perform the byte sequence conversion, and the host's byte sequence is different from the network's byte sequence, then the problem arises. For the CPU, assuming that it is consistent (and can only, it was determined at the design time) read from the high address and use it as the high data level, so all the data is wrong, so the Conversion Function opened up a memory, and the data sequence is completely flipped over, so that the CPU reads this data correctly.

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.