字节序分为 little-endian 和 big-endian,不同主机通信字节序转换过程:
主机字节序 -> 网络字节序 ->主机字节序
判断网络字节序:
#include<arpa/inet.h>
int isBigEndian()
{
int flag = 0;
short a = htons(0x0102);
char* p = &a;
if(*p==0x01)
{
flag = 1;
}
return flag;
}
网络字节序为big-endian
判断主机字节序:
int isBigEndian()
{
int flag = 0;
short a = 0x0102;
char* p = &a;
if(*p==0x01)
{
flag = 1;
}
return flag;
}
我的本地主机字节序为little-endian: