网络字节流和主机字节流

来源:互联网 发布:程序员需要学英语吗 编辑:程序博客网 时间:2024/06/01 10:21
字节流分为两类
little edition(LE)
big edition(BE)

0x123456 在两种字节流中的储存方式:

地址               LE          BE
0x0000         56          12
0x0001         34          34
0x0002         12          56

主机字节流根据cpu类型而定

网络字节流采用BE格式

为了进行转换 bsd socket提供了转换的函数 有下面四个
htons 把unsigned short类型从主机序转换到网络序
htonl 把unsigned long类型从主机序转换到网络序
ntohs 把unsigned short类型从网络序转换到主机序
ntohl 把unsigned long类型从网络序转换到主机序
(h:hos,n:network,s:short,l:long)
0 0