Byte order and size end, byte order size end

Source: Internet
Author: User

Byte order and size end, byte order size end
Why is there a byte order problem?

1. memory uses 8 bits as an address unit: the early processor address uses 8 bits as a unit (8-bit processor). That is to say, 8 bits of data can be accessed at a time, and then 16 bits are generated, 32-bit or even 64-bit processors, but to be compatible with the oldest 8-bit processors, 8-bit is used as an address unit.

2. more than 8-bit processors, such as 32-bit processors, although each 8-bit (one byte) stores data in the same way, however, for data with multiple bytes of data structure such as integer data, the data stored in each byte is the same, but the storage methods (I .e. several bytes of order) are different between bytes.

3. Other historical reasons.


Different processor structures have different storage orders for each byte of the big data type such as integer data in the memory. In this way, when the processor platforms of different architectures communicate with each other, the issue of byte order occurs.

Communication between different processors is generally conducted through network communication, so We unified the network byte order (using the large-end byte order ).


Key points:

Byte order: the storage order of each byte in the memory of the big integer data type (such as int, short, and char is not counted.

Generally, network communication requires attention.


Big end and Small End

There are two types of byte order: large and small.

Big end: data is stored at a high level in a low address, and its position is in a high address. For example, 0x12345678 is stored in the memory as 12 34 56 78 (address, from left to right to low)

Small End: data is stored at a low address, and stored at a high address. For example, 0x12345678 is stored in the memory as 78 56 34 12 (address, from left to right to low)


The following lists platforms of various sizes.



Several Functions of byte order conversion:

<pre name="code" class="cpp">#include <arpa/inet.h>uint32_t htonl(uint32_thostint32);Returns: 32-bit integer in network byte orderuint16_t htons(uint16_thostint16);Returns: 16-bit integer in network byte orderuint32_t ntohl(uint32_tnetint32);Returns: 32-bit integer in host byte orderuint16_t ntohs(uint16_tnetint16);Returns: 16-bit integer in host byte order


 

16 indicates 16 bits, and short occupies 16 bits. 32 indicates 32 bits, and unsigned int occupies 32 bits.

H indicates host, and n indicates network. L indicates long, s indicates short

In network communication, only the address and port number need to be converted in byte order, because other data is transmitted as strings.

Why is there a byte order problem?

1. memory uses 8 bits as an address unit: the early processor address uses 8 bits as a unit (8-bit processor). That is to say, 8 bits of data can be accessed at a time, and then 16 bits are generated, 32-bit or even 64-bit processors, but to be compatible with the oldest 8-bit processors, 8-bit is used as an address unit.

2. more than 8-bit processors, such as 32-bit processors, although each 8-bit (one byte) stores data in the same way, however, for data with multiple bytes of data structure such as integer data, the data stored in each byte is the same, but the storage methods (I .e. several bytes of order) are different between bytes.

3. Other historical reasons.

Different processor structures have different storage orders for each byte of the big data type such as integer data in the memory. In this way, when the processor platforms of different architectures communicate with each other, the issue of byte order occurs.

Communication between different processors is generally conducted through network communication, so We unified the network byte order (using the large-end byte order ).

Key points:

Byte order: the storage order of each byte in the memory of the big integer data type (such as int, short, and char is not counted.

Generally, network communication requires attention.

Big end and Small End

There are two types of byte order: large and small.

Big end: data is stored at a high level in a low address, and its position is in a high address. For example, 0x12345678 is stored in the memory as 12 34 56 78 (address, from left to right to low)

Small End: data is stored at a low address, and stored at a high address. For example, 0x12345678 is stored in the memory as 78 56 34 12 (address, from left to right to low)

The following lists platforms of various sizes.

Several Functions of byte order conversion:

<pre name="code" class="cpp">#include <arpa/inet.h>uint32_t htonl(uint32_thostint32);Returns: 32-bit integer in network byte orderuint16_t htons(uint16_thostint16);Returns: 16-bit integer in network byte orderuint32_t ntohl(uint32_tnetint32);Returns: 32-bit integer in host byte orderuint16_t ntohs(uint16_tnetint16);Returns: 16-bit integer in host byte order


 

16 indicates 16 bits, and short occupies 16 bits. 32 indicates 32 bits, and unsigned int occupies 32 bits.

H indicates host, and n indicates network. L indicates long, s indicates short



In network communication, only the address and port number need to be converted in byte order, because other data is transmitted as strings.






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.