The htonl function converts a u_long from host to TCP/IP network byte order (which is big-endian).
u_long htonl( u_long hostlong );
The htonl function returns the value in TCP/IP's network byte order.
The htonl function takes a 32-bit number in host byte order and returns a 32-bit number in the network byte order used in TCP/IP networks.
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winsock2.h.
Library: Use Ws2_32.lib.
The ntohl function converts a u_long from TCP/IP network order to host byte order (which is little-endian on Intel processors).
u_long ntohl( u_long netlong );
The ntohl function always returns a value in host byte order. If the netlong parameter was already in host byte order, then no operation is performed.
The ntohl function takes a 32-bit number in TCP/IP network byte order and returns a 32-bit number in host byte order.
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winsock2.h.
Library: Use Ws2_32.lib.
The htons function converts a u_short from host to TCP/IP network byte order (which is big-endian).
u_short htons( u_short hostshort );
The htons function returns the value in TCP/IP network byte order.
The htons function takes a 16-bit number in host byte order and returns a 16-bit number in network byte order used in TCP/IP networks.
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winsock2.h.
Library: Use Ws2_32.lib.
The ntohs function converts a u_short from TCP/IP network byte order to host byte order (which is little-endian on Intel processors).
u_short ntohs( u_short netshort );
The ntohs function returns the value in host byte order. If the netshort parameter was already in host byte order, then no operation is performed.
The ntohs function takes a 16-bit number in TCP/IP network byte order and returns a 16-bit number in host byte order.
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winsock2.h.
Library: Use Ws2_32.lib.
不同的CPU有不同的字节顺序类型,这些字节顺序类型指的是整数在内存中保存的顺序,即主机字节顺序。常见的有两种:
序号
|
英文名
|
中文名
|
描述
|
1
|
big-endian
|
大尾顺序
|
地址的低位存储值的高位
|
2
|
little-endian
|
小尾顺序
|
地址的低位存储值的低位
|
e.g:
#include "stdafx.h"
#include "iostream.h"
#include
using namespace std;
//#pragma comment(lib,"ws2_32.lib")//菜单栏-->Project-->Settings-->Link标签页-->Object/library modules-->加入ws2_32.lib
#include "winsock2.h"
//#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
int iNumh = 0x12345678;
int iNuml = htonl(iNumh);
int iNums = htons(iNumh);
cout<<"iNumh="<
&iNumh的值为:0012FF44 78 56 34 12
其中0x0012ff44、0x0012ff45、0x0012ff46、0x0012ff47这四个单元的值依次为:78、 56、 34、 12,即0x12345678这个数的高位部分存放在高位地址中,低位部分存放在低位地址中,即小尾顺序。
&iNuml的值为:0012FF40 12 34 56 78
其中0x0012ff40、0x0012ff41、0x0012ff42、0x0012ff43这四个单元的值依次为:12、 34、 56、 78,即把原数0x12345678的高位部分存放在低位地址中,低位部分存放在高位地址中。由此可见,如果一个数以小尾顺序存储,经htonl函数调用后这个数的高地位字节会完全颠倒过来成为一个新的数。这个新的数在机器内部其实还是以小尾顺序存储的,但是相对于原来的数而言相当于是变成大尾顺序。