Ntohs, Ntohl, htons,htonl comparison, detailed

Source: Internet
Author: User
Tags htons

Ntohs =net to host short int 16 bits

Htons=host to net short int 16 bits

Ntohs =net to host long int 32 bits

Htonl=host to net long int 32 bits

Briefly:
Converts an unsigned short integer number from network byte order to host byte order.

#include

U_short PASCAL FAR ntohs (u_short netshort);

Netshort: A 16-digit number expressed in network byte order.

Comments:
This function converts a 16-digit number from network byte order to host byte order.

return value:
Ntohs () Returns a number expressed in host byte order.



Converts the unsigned short integer number of hosts to network byte order.

#include

U_short PASCAL FAR htons (u_short hostshort);

Hostshort: 16 Digits of host byte order expression.

Comments:
This function converts a 16-digit number from host byte order to network byte order.

return value:
Htons () returns the value of a network byte order.

These 2 functions provide a conversion of host byte order and network byte order

Different CPUs have different byte-order types These are the order in which integers are stored in memory. This is called the host sequence.
The most common of these are two
1. Little endian: Storing low order bytes at the start address
2. Big endian: Storing High-order bytes at the start address

LE Little-endian
The byte sequence that best fits the human mind
Low address Low store value
High Address High store value
How to say is the most consistent with the human mind of the byte sequence, because from the first impression of people
Low value is small, it should be placed in the memory address of the small place, that is, memory address low
Conversely, the high value should be placed in the memory address of the place, that is, high memory address

Be Big-endian
Most intuitive byte-order
Low Address store value high
Low address High store value
Why the intuitive, do not consider the corresponding relationship
Only need to write the memory address from left to right in the order from low to high
Write out the values in the order of the usual highs to lows
In contrast, a byte padding of one byte

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 CPU is a Little-endian byte sequence.

Network byte order is a set of data representation format in TCP/IP, which is independent of specific CPU type, operating system, etc., so that data can be interpreted correctly when transmitting between different hosts. The network byte order takes the big endian sort way.

In order to convert the BSD socket provided the converted function has the following four
Htons converts the unsigned short type from the host sequence to the network sequence
HTONL converts the unsigned long type from the host sequence to the network sequence
Ntohs converts the unsigned short type from the network sequence to the host sequence
Ntohl converts the unsigned long type from the network sequence to the host sequence

In systems using little endian, these functions convert the byte order
These functions are defined as empty macros in systems that use the big endian type

Also in the development of Web programs or cross-platform development should be careful to ensure that only one byte-order or two-party interpretation is not the same will produce bugs.

Note:
1, network and host byte conversion function: Htons ntohs htonl Ntohl (S is short L is long H is host n is network)
2, different CPUs run different operating systems, the byte sequence is also different, see the table below.
Processor OS byte sort
Alpha all Little endian
Hp-pa NT Little Endian
Hp-pa UNIX Big Endian
Intelx86 all Little endian <-–x86 system is a small-end byte-order system
motorola680x () All big endian
MIPS NT Little Endian
MIPS UNIX Big Endian
PowerPC NT Little Endian
PowerPC non-NT big endian &LT;-–PPC system is a big-endian byte-order system
rs/6000 UNIX Big Endian
SPARC UNIX Big Endian
IXP1200 Arm Core All Little endian

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.