字节序,顾名思义,指字节在内存中存储的顺序。
- 小端字节序(Little endinan),数值低位存储在内存的低地址,高位存储在内存的高地址;
- 大端字节序(Big endian),数值高位存储在内存的低地址,低位存储在内存的高地址。
网络字节序,是TCP/IP中规定好的一种数据表示格式,它与具体的CPU类型、操作系统等无关,从而可以保证数据在不同主机之间传输时能够被正确解释。网络字节顺序采用big endian排序方式
#include <arpa/inet.h> uint32_t htonl(uint32_t hostlong); //把uint32_t类型从主机序转换到网络序 uint16_t htons(uint16_t hostshort); //把uint16_t类型从主机序转换到网络序 uint32_t ntohl(uint32_t netlong); //把uint32_t类型从网络序转换到主机序 uint16_t ntohs(uint16_t netshort); //把uint16_t类型从网络序转换到主机序