[C + +] Go CPU byte sequence network sequence host sequence big end small terminal

Source: Internet
Author: User
Tags define function integer numbers htons

Original posts: http://www.cnblogs.com/darktime/p/3298075.html

Different CPUs have different byte-order types these byte-order refers to the order in which integers are stored in memory this is called the host order.
Two of the most common
1. Little endian: Storing low-order bytes at the start address
2. Big Endian: Stores high-order bytes at the start address

LE Little-endian
The byte order that best fits the human mind
Low store value of address low
High-level storage value of address high
How to say is the most consistent byte-order of human thinking, because from the first impression of people
Low value, should be placed in the memory address is small, that is, memory address low
Conversely, the high value should be placed in the memory address is large, that is, memory address high

Be Big-endian
The most intuitive byte-order
High position of address low store value
Address High store value low
Why speak intuitively, don't consider correspondence
Just write the memory address from left to right in order from low to high
Write the values in the usual high-to-low order
In contrast, a byte-by-byte padding

Example: How to store a double-character 0x01020304 (DWORD) in memory

Memory address
4000 4001 4002 4003
LE 04 03 02 01
be 01 02 03 04

Example: If we write 0X1234ABCD to memory starting with 0x0000, the result is
Big-endian Little-endian
0x0000 0x12 0xCD
0x0001 0x23 0xAB
0x0002 0xAB 0x34
0x0003 0xCD 0x12
The x86 series CPUs are Little-endian byte-order.

Network byte order is a well-defined data representation format in TCP/IP, which is independent of the specific CPU type, operating system and so on, so that the data can be interpreted correctly when transferring between different hosts. The network byte order takes the big endian sort method.

Principle of host byte-order conversion to network byte sequence:
The network byte sequence is arranged in low-byte high-order mode,
While the X86 host byte sequence uses low-byte in the lower-order way
In fact, as long as the exchange of the network byte sequence and host byte sequence conversion can be achieved,
So for the above function, htonl and Ntohl are the same.

    1. H Host
    2. N NET Network
    3. L Long Length shaping
    4. s short Plastic
    5. Xtox is the conversion of the host and network order of the data storage sequence
Htonl and htons respectively convert unsigned long and unsigned short integer numbers into a unified network byte-order number defined by the TCP/IP protocol, which is the big head order. Computers in different systems store numbers while some put the lowest bits in front, while others are the opposite, the so-called big head and small heads. Digital access to the Internet should first be converted to a unified htonl or htons. htonl (convert 32-bit host character order to network character order)
Defines the function unsigned long int htonl (unsigned long int hostlong);

The function Description htonl () Converts the 32-bit hostlong specified by the parameter to the network character order.

The return value returns the corresponding network character order.
htons (convert 16-bit host character order to network character order)
Defines the function unsigned short int htons (unsigned short int hostshort);

The function Description htons () Converts the 16-bit hostshort specified by the parameter to the network character order.

The return value returns the corresponding network character order. Ntohl (converts 32-bit network character order to host character order)
Defines the function unsigned long int ntohl (unsigned long int netlong);

The function Description Ntohl () is used to convert the 32-bit netlong specified by the parameter to the host character order.

The return value returns the corresponding host character order.


Ntohs (converts 16-bit network character order to host character order)

Defines the function unsigned short int ntohs (unsigned short int netshort);

The function Description Ntohs () is used to convert the 16-bit netshort specified by the parameter to the host character order.

The return value returns the corresponding host order. For example, the network byte is xx U_short A; a=0100; Since the host is from high byte to low byte, it should be converted after A=ntohs (0001);//such a=0001; Assuming you already have a SOCKADDR_IN structure ina, you have an IP address "132.241.5.10" to store in which you will need to use the function inet_addr () to convert the IP address from the point format to the unsigned long integer. Here's how to use it:
INA.SIN_ADDR.S_ADDR = inet_addr ("132.241.5.10″");
Note that the address returned by INET_ADDR () is already in network byte format, so you no longer have to call function htonl ().
inet_addr (Turn network address into binary number)
Defines the function unsigned long int inet_addr (const char *CP);

The function Description inet_addr () is used to convert the network address string referred to by the parameter CP to the binary number used by the network. A network address string is a string of numbers and dots, for example: "163.13.132.68".

The return value succeeds when the corresponding network binary number is returned, and the failure returns-1.

Ntoa:network to Ascii//aton:ascii to network
Inet_aton (the number that turns the network address into a network binary)
Defines the function int Inet_aton (const char * cp,struct in_addr *INP);

The function Description Inet_aton () is used to convert the network address string referred to by the parameter CP into a binary number used by the network, and then stored in the IN_ADDR structure referred to by the parameter INP.
The structure in_addr is defined as follows
struct IN_ADDR
{
unsigned long int s_addr;
};

The return value succeeds returns a value other than 0, and fails returns 0.



Inet_ntoa (converts a network binary number into a network address)
define function char * INET_NTOA (struct in_addr in);

The function Description Inet_ntoa () is used to convert the number of the network binary that the parameter in refers to into a network address, and then returns a pointer to this network address string. (Convert the network address to ".") Dot-separated string format)

Returns a string pointer if the return value succeeds, or null if failed.

[C + +] Go CPU byte sequence network sequence host sequence big end small terminal

Related Article

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.