htonl(),htons(),ntohl(),ntons()--大小端模式转换函数
来源:互联网 发布:linux gcc 链接动态库 编辑:程序博客网 时间:2024/05/19 02:28
htonl(),htons(),ntohl(),ntons()--大小端模式转换函数
不同机器内部对变量的字节存储顺序不同,有的采用大端模式(big-endian),有的采用小端模式(little-endian)。
大端模式是指高字节数据存放在低地址处,低字节数据放在高地址处。
小端模式是指低字节数据存放在低地址处,高字节数据放在高地址处。
在网络上传输数据时,由于数据传输的两端可能对应不同的硬件平台,采用的存储字节顺序也可能不一致,因此 TCP/IP 协议规定了在网络上必须采用网络字节顺序(也就是大端模式) 。
通过对大小端的存储原理分析可发现,对于 char 型数据,由于其只占一个字节,所以不存在这个问题,这也是一般情况下把数据缓冲区定义成 char 类型 的原因之一。对于 IP 地址、端口号等非 char 型数据,必须在数据发送到网络上之前将其转换成大端模式,在接收到数据之后再将其转换成符合接收端主机的存储模式。
Linux 系统为大小端模式的转换提供了 4 个函数,输入 man byteorder 命令可得函数原型:
- #include <arpa/inet.h>
- uint32_t htonl(uint32_t hostlong);
- uint16_t htons(uint16_t hostshort);
- uint32_t ntohl(uint32_t netlong);
- uint16_t ntohs(uint16_t netshort);
#include <arpa/inet.h>uint32_t htonl(uint32_t hostlong);uint16_t htons(uint16_t hostshort);uint32_t ntohl(uint32_t netlong);uint16_t ntohs(uint16_t netshort);
htons 表示 host to network short ,用于将主机 unsigned short 型数据转换成网络字节顺序;
htonl 表示 host to network long ,用于将主机 unsigned int 型数据转换成网络字节顺序;
ntohl、ntohs 的功能分别与 htonl、htons 相反。
- htonl(),htons(),ntohl(),ntons()--大小端模式转换函数
- htonl(),htons(),ntohl(),ntons()--大小端模式转换函数
- htonl(),htons(),ntohl(),ntons()--大小端模式转换函数
- htonl(),htons(),ntohl(),ntons()
- 大小端转换htonl、ntohl、htons、ntohs
- 字节序大小端转换、模拟htons、htonl、ntohs、ntohl
- htonl(), ntohl(), htons(), ntohs() 函数
- htonl(), ntohl(), htons(), ntohs() 函数
- htonl(), ntohl(), htons(), ntohs() 函数
- htonl\htons\ntonl\ntons
- 网络字节转换函数htons htonl ntohs ntohl
- 网络字节转换函数 htons(), htonl(), ntohs(), ntohl()
- 高低字节序转换(htonl、ntohl、htons、ntohs函数)
- htons ntohs htonl ntohl 大小端字节变换
- 网络通信之htonl()、htons()、ntohl()、ntohs()四个函数以及小端大端(小尾大尾)模式
- 模拟htonl、ntohl、htons、ntohs函数实现
- htonl、ntohl、htons、ntohs函数实现
- htonl、htons、ntohl、ntohs函数实现
- 获取当前的系统数据
- VC 文件流读写
- 块设备驱动实战基础篇一 (170行代码构建一个逻辑块设备驱动)
- 十六周项目一
- unix strip命令
- htonl(),htons(),ntohl(),ntons()--大小端模式转换函数
- ibates的sqlmap 配置收藏
- Flex错误码列表
- Struts2使用Kindeditor4.0.3在线编辑器--上传图片、视频、FLASH、附件
- iOS杂谈16—iOS开源资源收集
- Activity 中动态增加控件
- Windows自带的小巧的调试利器--Dr.Watson
- GTK+与MFC不完全对比GTK+与MFC不完全对比
- typedef char * pStr的问题和const char*, char const*, char*const的区别