大小端转换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有不同的字节顺序类型,这些字节顺序类型指的是整数在内存中保存的顺序,即主机字节顺序。常见的有两种:
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函数调用后这个数的高地位字节会完全颠倒过来成为一个新的数。这个新的数在机器内部其实还是以小尾顺序存储的,但是相对于原来的数而言相当于是变成大尾顺序。
- 大小端转换htonl、ntohl、htons、ntohs
- 字节序大小端转换、模拟htons、htonl、ntohs、ntohl
- htons ntohs htonl ntohl 大小端字节变换
- htons(), htonl(), ntohs(), ntohl()
- ntohs(),ntohl(),htons(),htonl(),
- htons(), htonl(), ntohs(), ntohl()
- htons(), htonl(), ntohs(), ntohl()
- htons,htonl,ntohs,ntohl
- htons(), htonl(), ntohs(), ntohl()
- htonl(), ntohl(), htons(), ntohs().
- htons,htonl,ntohs,ntohl
- htonl ntohl htons ntohs
- htons ntohs htonl ntohl
- htonl、htons、ntohl、ntohs
- ntohs, ntohl, htons,htonl
- ntohs,htons,ntohl,htonl
- ntohl();ntohs();htons();htonl() 说明
- htonl(), ntohl(), htons(), ntohs() 函数
- makefile作用
- unity3d使用摄像机观察对象
- 基于hibernate4的BaseDao接口
- 每天一个linux命令(9):touch 命令
- 多线程中的Future模式及其在高性能IO框架netty中的应用
- 大小端转换htonl、ntohl、htons、ntohs
- android社区
- Zookeeper(三)客户端脚本
- 怎样用ECharts绘制一个简单的柱形图表
- 坚持#第136天~循循渐进!
- Angular与.NetMVC一起使用
- 自创的 TplTool,一个简单易用的前端模版JS组件(2016年小小成果)
- JavaEE Spring框架学习笔记(AOP基本应用实例)
- C++程序员学习发展方向分析和指导,学习参考!