大小端转换htonl、ntohl、htons、ntohs
2016-12-28 23:30
453 查看
htonl
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 );
Parameters
hostlong [in] 32-bit number in host byte order.Return Values
The htonl function returns the value in TCP/IP's network byte order.Remarks
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.Requirements
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.
ntohl
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 );
Parameters
netlong [in] 32-bit number in TCP/IP network byte order.Return Values
The ntohl function always returns a value in host byte order. If thenetlong parameter was already in host byte order, then no operation is performed.
Remarks
The ntohl function takes a 32-bit number in TCP/IP network byte order and returns a 32-bit number in host byte order.Requirements
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.
htons
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 );
Parameters
hostshort [in] 16-bit number in host byte order.Return Values
The htons function returns the value in TCP/IP network byte order.Remarks
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.Requirements
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.
ntohs
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 );
Parameters
netshort [in] 16-bit number in TCP/IP network byte order.Return Values
The ntohs function returns the value in host byte order. If thenetshort parameter was already in host byte order, then no operation is performed.
Remarks
The ntohs function takes a 16-bit number in TCP/IP network byte order and returns a 16-bit number in host byte order.Requirements
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 <iomanip>
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<<endl;
cout<<"iNuml="<<iNuml<<endl;
cout<<"iNumh="<<iNumh<<endl;
cout<<"iNums="<<iNuml<<endl;
cout<<"iNumh="<<hex<<iNumh<<"and iNumh's addr="<<&iNumh<<endl;
cout<<"iNuml="<<hex<<iNuml<<"and iNuml's addr="<<&iNuml<<endl;
cout<<"iNumh="<<hex<<iNumh<<"and iNumh's addr="<<&iNumh<<endl;
cout<<"iNums="<<hex<<iNuml<<"and iNums's addr="<<&iNums<<endl;
system("pause");
return 0;
}
&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函数调用后这个数的高地位字节会完全颠倒过来成为一个新的数。这个新的数在机器内部其实还是以小尾顺序存储的,但是相对于原来的数而言相当于是变成大尾顺序。
相关文章推荐
- 网络字节顺序和主机字节顺序的转换(htons ntohs htonl ntohl)
- 字节序大小端转换、模拟htons、htonl、ntohs、ntohl
- 网络通信之htonl()、htons()、ntohl()、ntohs()四个函数以及小端大端(小尾大尾)模式
- 网络字节顺序和主机字节顺序的转换(htons ntohs htonl ntohl)
- 网络字节顺序和主机字节顺序的转换(htons ntohs htonl ntohl)
- 网络字节顺序和主机字节顺序的转换(htons ntohs htonl ntohl)
- 网络字节序与主机字节序的转换(htonl,htons,inet_addr,ntohl,ntohs,inet_ntoa)
- htons ntohs htonl ntohl 大小端字节变换
- 网络字节顺序和主机字节顺序的转换(htons ntohs htonl ntohl)
- 网络字节转换函数 htons(), htonl(), ntohs(), ntohl()
- htons(), ntohs(),htonl(),ntohl(), inet_addr() ,inet_ntoa() || bzero()
- ntohs(),ntohl(),htons(),htonl(),
- htons(), htonl(), ntohs(), ntohl()详解
- Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa)
- 实例验证网络字节序、主机字节序转换函数 htonl htons ntohl ntohs的用法以及需要注意的问题
- ntohs, ntohl, htons,htonl的比较和详解
- ntohs, ntohl, htons,htonl的比较和详解
- ntohs, ntohl, htons,htonl的比较和详解
- htons,htonl,ntohs,ntohl
- ntohs, ntohl, htons,htonl的比较和详解