网络序与主机序

字节序:

小端格式(Little-Endian):将低位字节数据存储在低地址。
大端格式(Big-Endian):将高位字节数据存储在低地址。

网络协议指定了通讯字节序为大端格式。

整型数示例

字节序转换函数:

#include <arpa/inet.h> /* need to include */

uint32_t htonl(uint32_t hostint32); /* host to net long(32 bit) */
uint16_t htons(uint16_t hostint16); /* host to net long(16 bit) */
uint32_t ntohl(uint32_t netint32);  /* net to host long(32 bit) */
uint16_t ntohs(uint16_t netint16);  /* net to host long(16 bit) */
原文地址:https://www.cnblogs.com/tongyishu/p/11819253.html