1. 基本概念
小端(little-endian):将低序字节存储在起始地址(低位存储在低地址)
大端(big-endian):将高序字节存储在起始地址(高位存储在低地址)
示例:
对于一个数0x1234
小端机器:低地址存储0x34,高地址存储0x12
大端机器:低地址存储0x12,高地址存储0x34
注:
网络字节序为小端;
目前网络编程的套接口地址结构的部分成员采用网络字节序(小端)进行维护;
2. 字节排序函数
uint16_t htons(uint16_t host16bitvalue);
uint32_t htonl(uint32_t host32bitvalue);
-----------------------------------------------------
uint16_t ntohs(uint16_t net16bitvalue);
uint32_t ntohl(uint32_t net32bitvalue);
其中:h-host n-network s-short l-long