htons():ntohl():ntohs():htons()函数
来源:互联网 发布:dj混音软件 编辑:程序博客网 时间:2024/06/04 01:07
htons()/ntohl()/ntohs()/htons()函数
在C/C++网络程序中,往往会需要考虑网络序与字节序。而这四个函数就是用来转换网络序与字节序
先来看一下这四个函数的英文意思
- htonl()
Host to Network Long
- htons()
Host to Network Short
- ntohl()
Network to Host Long
- ntohs()
Network to Host Short
譬如htonl,其实 就是 H to N L,其实我之前用了很久,也是现在整理的时候发现命名是这个规律,当时最初用的时候还觉得这个名字怎么这么奇怪
网络序与主机序
网络序,NBO,Network Byte Order,字节顺序就是高位字节在左,低位字节在右
主机序,HBO,Host Byte Order,这个字节顺序简单来说有两种,一种是大端,一种是小端,大端顺序和网络序顺序一样,小端则是高位字节在右,地位字节在左,不过现在主流由于是intel CPU,所以是小端
举例
short型数0x1234网络序0x12 34大端主机序0x12 34小端主机序0x34 12long型数0x12345678网络序0x12 34 56 78大端主机序0x12 34 56 78小端主机序0x78 56 34 12
测试代码
int main(){ printf("%d\n", htons(16)); return 0;}
打印结果为4096
解释
数字16的16进制表示是0x0010,数字4096的16进制表示是0x1000.而运行该代码的是Inter机器,Intel机器是小尾端,所以存储16的实际存储是0x 10 00,经过htons处理后,结果为0x 00 10,所以显示是4096,但是如果在大尾端的机器上运行该代码,那么结果就会是16
注意:数字如果小于等于一个字节,不能使用htons等之类的函数。
0 0
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons():ntohl():ntohs():htons()函数
- htons(), ntohl(), ntohs(),htons()
- htons(), ntohl(), ntohs(),htons()
- htons(), ntohl(), ntohs(),htons()
- 为什么需要htons(), ntohl(), ntohs(),htons() 函数
- 为什么需要htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数网络收集
- htons(), ntohl(), ntohs(),htons() 函数网络收集
- Web服务器、Web容器
- Kuchiguse (20)
- 集训队专题(5.1)1007 All Friends
- ContextLoaderListener作用详解
- hdu 1212 Big Number(对大数进行取余)
- htons():ntohl():ntohs():htons()函数
- 点击html按钮实现连接功能
- 多线程中的互斥与同步机制
- javaWeb安全篇(1)——防盗链技术的实现
- android截屏
- Intel处理器Family、Model、Stepping等的学习
- Nginx配置文件nginx.conf中文详解
- oracle之空值(NULL)和空值函数使用
- POJ 2456 Aggressive cows