What is a byte order? Refers to the order in which integers are saved in memory. There are two kinds of byte order: A.little Endinan Low store in memory, high address in memory, B.big endian high store in memory low address, low store in memory;
A simple example: DWORD dwcount = 0x01020304; Such a double-byte variable is distributed in memory as follows 0013ff70 04 03 02 01 (Note: The results of the experiment XPSP3 VC6.0, indicating that my machine is little Endinan order) And if the big endian order, it should be 0013ff70 01 02 03 04. With more x86 series of CPUs, are little Endinan sequence.
and network byte order This is a TCP/IP protocol defined in a data representation format, it is not related to your machine's CPU, the operating system and so on, so that the data in the network transmission, no matter how the kind can be interpreted correctly, the network byte order selected big endian. This gives the programmer to write the network program a problem, the programmer must convert the local data from the host byte sequence to the network byte sequence sent to the host, while the host must also transfer the received data from the network byte order to the cost of byte order, so as to ensure that the two sides correctly send and receive data, or generate a bug.
WINAPI provides four functions for network byte order to native byte order and native byte order to network byte order conversion: htons: Convert unsigned short from host to network order HTONL: Convert unsigned long from host to network order Ntohs: Convert the unsigned short type from the network sequence to the host order Ntohl: Convert the unsigned long type from the network sequence to the host order (these four APIs can be remembered, s means short,l means long,n represents the network, H = host)
In addition: The char type is a problem with no byte order, only a byte-order problem with data types larger than one byte. Many translation books translate little endinan into small-end mode, translating big endian into large-endian mode.
[End]
Network byte order & host byte order