Communication Protocol-byte order, communication protocol byte

Source: Internet
Author: User

Communication Protocol-byte order, communication protocol byte

Recently, communication protocols have come into byte order. I remember it was too long to remember it when I went to school. Now I am talking about byte order here;

We all know that data is convertedBytes for storageAnd our programs or stored data are usually composedConsists of multiple bytesAnd different computer or device storage rulesInconsistentAs a result, we need to know what the device's byte storage (byte order) Rules are when transmitting data between multiple computers and devices. This is why we need to understand the byte order, this must be clear about network programming and game programming;

For example: In our computer programming language, there are various data types: byte, int, short, long, etc.

A byte is composed of eight bits, a short is composed of two bytes, and an int is composed of four bytes, when we store byte, short, int, and other data or transmit data over the network, we needConvertByteFor storage or transmissionWhen an int is converted into a byte arrayData storage SequenceIn bytes.

For example, for int type data: 33818120, The hexadecimal value is 0x02040608.

After converting to a byte array, the byte data is: byte [4] =}

The data sequence in the byte array is called:Byte order

Why?What about the byte sequence during storage or network programming? In network programming, for example, if the data we send is byte [4] = {,}, and the receiving end does not know our byte order, it uses the byte order to receive data in the opposite order: byte [4] = {08,06, 04,02}. As a result, the data received by the acceptor becomes invalid due to an error. The data we send is 33818120, however, because he does not know our byte order, the received data is 134611970;

In English, this word is "endian", which can be translated into an end sequence or a byte sequence. Here we call it "byte sequence ], it is said that the term endian originated from a 18th century novel "Gulliver's Travels", in which: "When I eat eggs, I break them from the big end of the eggs or from the small end of the eggs." I thought there was a rebellion; in 1980, a network protocol developer used the term for the first time in his famous paper On Holy Wars and a Plea for Peace.

Literally, the byte order is:Byte storage SequenceAs mentioned above, the byte order is actually the order in which program objects and data are stored on the computer. Generally, the byte order is divided:Small byte order (Little-Endian), Big-Endian), Mixed byte order (Mixed-Endian)Three TypesThe mixed byte order is rarely used. The TCP/IP layer protocol uses Big-Endian.Network byte order;

The length of four bytes with block address space is as follows:

 

Example: decimal number 33818120 hexadecimal: 0x02040608

In the address space, the storage is in the following format:

 

Large byte order (Little-Endian) Storage format in the address space:

Big-Endian) Storage format in the address space:

 

Large byte order: The data is stored at the High Level in the address space, and the data is stored at the High Level in the address space.

Small byte order: Low-level data is stored in the address space, while high-level data is stored in the address space.

In java, Big-Endian is used to store data, while C/C ++ is related to CPU. Therefore, in network programming, one end is a java program, at one end, the program in C/C ++ needs to unify the transmitted data's byte order (Endian) during transmission, all of which use Little-Endian) or Big-Endian. Otherwise, the data received by one end may be invalid.

First Article address: Solinx
Http://www.solinx.co/archives/164

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.