字节序一共有两种方式:大端,小端;
大端:将高序字节存在起始地址;
小端:将低序字节存放在起始地址;
网络字节序统一大端存储方式;
本地字节序是根据本地的cpu来的,x86的cpu都是小端存储的。
转换方式:
htons 把unsigned short类型从主机序转换到网络序
htonl 把unsigned long类型从主机序转换到网络序
ntohs 把unsigned short类型从网络序转换到主机序
ntohl 把unsigned long类型从网络序转换到主机序
win 下包含在库 winsock.h 中;
在Linux系统下:
#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
有些系统包含的头文件是 <netinet/in.h> 而不是 <arpa/inet.h>
字节序及转换
最新推荐文章于 2024-06-04 15:57:17 发布