Size-end byte sequence

Source: Internet
Author: User

The bytecode issue involves the hardware architecture. Currently, it is mainly about Motorola's PowerPC series CPU and Intel's X86 series CPU.The PowerPC series uses big endian for data storage, while the x86 series uses little endian for data storage.. So what is big endian and little endian?

For ease of understanding, I have extracted a picture from the Inter manual. Haha.

Byte sequence refers to the storage order of data that occupies more than one byte in the memory. Generally, there are two types of byte sequence: small and large.The small-end byte sequence means that the low-byte data is stored at the low address in the memory, and the high-byte data is stored at the Internal High address. The large-end byte sequence is the high-byte data stored at the low address, low-byte data is stored at the high address.

The PC based on the iax86 platform is in the small byte order, while some embedded platforms are in the large byte order. Therefore, for data of more than 1 byte type such as Word, DWORD, and qword, the storage sequence should be changed on these embedded platforms. We usually think that the byte order transmitted in the air is the standard order of the network byte order. Considering the consistency with the Protocol and the intercommunication with other similar platform products, when sending data packets in the program, converts the host's byte order to the network's byte order, and the network's byte order to the host's byte order at the receiving data packet.

Text descriptions may be abstract. The following uses images to describe them. For example, the storage Order of the number 0x12345678 in two different bytes of CPU is as follows:

For 0x12345678,

Little endian: High address <-------> low: storage DATA 0x12 0x34 0x56 0x78

Big endian: High address <-------> low: storage DATA 0x78 0x56 0x34 0x12

Why should we pay attention to the issue of byte order? Of course, if the program you write runs only in a single-host environment and does not deal with other programs, you can ignore the existence of the byte sequence. But what if your program needs to interact with other programs? In C/C ++ programming, the data storage sequence is related to the CPU of the compilation platform, while in Java programming, the only way to store data is big endian. Imagine what will happen if you use a program written on the x86 Platform in C/C ++ to communicate with other Java programs? Take the above 0x12345678 as an example. The pointer pointing to 0x12345678 is passed to the Java program, because Java uses the big endian method to store data, naturally, it translates your data into 0x78563412. Therefore, it is necessary to convert the byte order before your C program passes on to the Java program.

All network protocols use big endian to transmit data.So sometimes we call the big endian method the byte order of the network. When two hosts communicate in different bytes, data must be converted to network bytes before transmission.
How to determine whether a rule is a small-end rule or a large-end rule:
Int x = 1;
If (* (char *) & X = 1) // The X pointer is forcibly converted to the char * type and then the value is the lowest byte value of Int.
Printf ("Little-Endian/N ");
Printf ("big-Endian/N ");

In addition:

1. Big-Endian, LITTLE-ENDIAN is CPU-related, each kind of CPU is not BIG-ENDIAN is little-Endian. In the IA architecture, the CPU is little-Endian, while the PowerPC, iSCSI, and Motorola processors. This is actually the so-called host byte sequence. The Network byte sequence is used to determine whether the data is large or small during network upload and transmission, and the network byte sequence on the internet is big-Endian. The so-called JAVA byte sequence refers to the order in which multi-byte data is stored in the Java VM, And the java byte sequence is also big-Endian.

2. therefore, when writing a communication program using C/C ++, before sending data, you must use htonl and htons to convert integer and short integer data from the host's byte order to the network's byte order, after receiving data, ntohl and ntohs must be called to convert the network byte sequence to the host byte sequence. If the communication party is a Java program and the communication party is a C/C ++ program, you must use the above methods to convert the byte order on the C/C ++ side, on the Java side, you do not need to do any processing, because the java byte order and the network byte order are both BIG-ENDIAN, as long as the C/C ++ side can be correctly converted (from the host order to the network order before sending, reverse conversion during receiving ). If both parties are in Java, you do not need to consider the issue of byte order.

Size-end byte sequence

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: 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.