大小端转换htonl、ntohl、htons、ntohs

来源:互联网 发布:约到优质学生炮 知乎 编辑:程序博客网 时间:2024/05/16 18:12

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
小尾顺序
地址的低位存储值的低位

     由于这个原因不同体系结构的机器之间无法通信,所以要转换成一种约定的数序,也就是网络字节顺序 。在PC开发中,Intel机器是小尾端,IBM等大尾端机器,则没有这种字节顺序转换,但为了程序的可移植性,也最好用这个函数。有ntohl、ntohs和htonl、htons函数可以用来进行网络字节和主机字节的转换。   

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函数调用后这个数的高地位字节会完全颠倒过来成为一个新的数。这个新的数在机器内部其实还是以小尾顺序存储的,但是相对于原来的数而言相当于是变成大尾顺序。

1 0
原创粉丝点击