Large-end mode and small-end mode, network byte sequence, and host byte sequence (Classic)

Source: Internet
Author: User
Tags htons

Http://wxxweb.blog.163.com/blog/static/135126900201022133740759/

Big-end mode and small-end Mode
I. Concepts and details
In computer systems of various systems, there are usually two types of byte storage mechanisms: Big-Endian and little-Endian, that is, big-end mode and small-end mode.
Let's review two keywords: MSB and LSB:
MSB: Most Significant Bit ------- maximum valid bit
LSB: least significant bit ------- minimum valid bit
Big-edian)
Big-Endian: MSB is stored on the lowest-end address.
For example, the double byte 0x1234 exists in the starting address 0x00002000 in the big-Endian mode:
| Data | <-- Address
| 0x12 | <-- 0x00002000
| 0x34 | <-- 0x00002001
In big-Endian, the sequence number arrangement in the bit sequence is as follows (taking the dual-byte 0x8b8a as an example ):
Bit | 0 1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 15
------ MSB----------------------------------LSB
Val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 |
+ -------------------------------------------- +
= 0x8 B 8
Little-Endian)
Little-Endian: The LSB is stored on the lowest-end address.
For example, the dual-byte 0x1234 exists in the starting address 0x00002000 in the form of little-Endian:
| Data | <-- Address
| 0x34 | <-- 0x00002000
| 0x12 | <-- 0x00002001
In little-Endian, sequence number Orchestration in bit sequence is the opposite of big-Endian. The method is as follows (taking the dual-byte 0x8b8a as an example ):
Bit | 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0
------ MSB-----------------------------------LSB
Val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 |
+ --------------------------------------------- +
= 0x8 B 8
2. Storage of arrays in large-end and small-end scenarios:
Taking unsigned int value = 0x12345678 as an example, we can use unsigned char Buf [4] to show the storage conditions of the two types of bytes respectively:
Big-Endian: Low-address storage is high, as shown below:
High address
---------------
Buf [3] (0x78) -- low
Buf [2] (0x56)
Buf [1] (0x34)
Buf [0] (0x12) -- high
---------------
Low address
Little-Endian: Low address storage, as follows:
High address
---------------
Buf [3] (0x12) -- high
Buf [2] (0x34)
Buf [1] (0x56)
Buf [0] (0x78) -- low
--------------
Low address

Iii. Big-end and small-end conversion method:
Convert big-Endian to little-endian as follows:
# Define bigtolittle16 (A) (uint16) (a) & 0xff00)> 8) | \
(Uint16) (a) & 0x00ff) <8 ))
# Define bigtolittle32 (A) (uint32) (a) & 0xff000000)> 24) | \
(Uint32) (a) & 0x00ff0000)> 8) | \
(Uint32) (a) & 0x0000ff00) <8) | \
(Uint32) (a) & 0x000000ff) <24 ))

Iv. Large-end and small-end detection methods:
How do I check whether the processor is big-Endian or little-Endian?
The storage order of union Union is that all members are stored from the low address. With this feature, the CPU can easily read and write the memory in the little-Endian or big-Endian mode.
Int checkcpuendian ()
{
Union
{
Unsigned int;
Unsigned char B;
} C;
C. A = 1;
Return (C. B = 1 );
}
/* Return 1: Little-Endian, return 0: Big-Endian */

Network byte sequence

1. Bit spaces in bytes are not affected by this order
For example, a byte of 1000 0000 (or expressed as hexadecimal 80 h) is represented in whatever order in its memory.
2. Only data types larger than 1 byte have the byte sequence problem.
For example, for byte A, this variable has only one byte length, so there is no byte order problem according to the previous one. Therefore, byte order refers to the relative order between bytes.
3. There are two types of data types in byte order:
For example, short B, which is a two-byte data type, then there is a problem with the relative sequence between bytes.
The byte order of the network is the order of "What you see is what you get. The bytes of Intel-type CPU are in the opposite order.
For example, the preceding short B = 0102 H (in hexadecimal notation, each two represents the width of a byte ). What we see is "0102". According to general mathematical knowledge, the number axis increases from left to right, that is, if the memory address increases from left to right, in the memory, the short B's byte order is:
01 02
This is the network byte sequence. The order we see is the same as the order in the memory!
The byte order is different. The order in the memory is 02 01.
Assume that the two byte streams of network data obtained through packet capture are: 01 02
If this represents two bytes, you do not need to consider the byte sequence.
If this represents a short variable, you need to consider the byte sequence. According to the "WYSIWYG" rule in the byte sequence of the network, the value of this variable is 0102.
Assume that the local host is of the Intel type, it is a little troublesome to indicate this variable:
Define the variable short X,
The byte stream address is PT, And the READ memory is
X = * (short *) pt );
The memory order of X is certainly 01 02.
According to the rules that are not "WYSIWYG", the memory order is obviously the same as that seen, so we need to change the positions of these two bytes.
The change method can be defined by yourself, but it is more convenient to use existing APIs.

Network byte sequence and host byte sequence
Nbo and HBO network byte order (Network byte order): stores data in a sequence from high to low, and uses a unified network byte order on the network to avoid compatibility issues. Host byte order (HBO, host byte order): different machines HBO are different. There are two types of computer data storage priorities related to CPU design: high byte priority and low byte priority. Data on the internet is transmitted over the network in high byte precedence. Therefore, for machines that store data in low byte precedence mode, data transmission over the Internet requires conversion.

Htonl ()
Brief description:
Converts the unsigned Length Integer of the host to the network byte sequence.
# Include <Winsock. h>
U_long Pascal far htonl (u_long hostlong );
Hostlong: 32-digit host bytes.
Note:
This function converts a 32-bit string from the host byte sequence to the network byte sequence.
Return Value:
Htonl () returns the value of a network byte sequence.

Inet_ntoa ()
Brief description:
Converts a network address to a string format separated.
# Include <Winsock. h>
Char far * Pascal far inet_ntoa (struct in_addr in );
In: An Internet host address structure.
Note:
This function converts an Internet address structure represented by the In parameter to a string separated by ".", such as "A. B. C. D. Note that the strings returned by inet_ntoa () are stored in the memory allocated by the Windows interface. The application should not assume how the memory is allocated. Before calling the next Windows interface of the same thread, the data will be valid.
Return Value:
If no error occurs, inet_ntoa () returns a character pointer. Otherwise, null is returned. The data should be copied before the next Windows interface call.

Some of the data transmitted over the network is in the same order as the local bytes, while others are completely different. To ensure data consistency, you must convert the local data to the format used on the network, then, the data is sent and received in the same way. After conversion, the data is used. The basic library function provides such a function for byte conversion, for example, with htons () htonl () ntohs () ntohl (), here n represents network, H represents host, htons () htonl () is used to convert local bytes to network bytes, s indicates short, that is, 2-byte operation, and l indicates long, that is, 4-byte operation. Same ntohs () ntohl (
) Is used to convert network bytes to the local format.

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.