1.利用联合体所有成员起始位置一致
bool isLittleEndian()
{
union isLittle_Endian
{
int i;
char c;
};
isLittle_Endian check;
check.i = 1;
return check.c == 1;
}
O "是小端模式吗? " << isLittleEndian();
1.利用强制类型转换
bool isLittleEndian2()
{
int val = 0x12345678;
short *s = (short *)&val;
return s[1] == 0x1234;
}
O "是小端模式吗? " << isLittleEndian2();