简单来说,小端就是看起来不顺眼的那个。
0x12345678,存储时也这么存储 12 34 56 78,看起来顺眼,是大端。
0x12345678,存储时这样存储 78 56 34 12,看起来不顺眼,是小端。
切记:小端看起来不顺眼。
判断大小端方法:
1.char 指针读取int数据一个Byte方法:
int IsBigEndian()
{
int i = 0x12345678;
unsigned char *p = (unsigned char*)(&i);
if(*p == (char)0x12) return 1;//12 34 56 78这么存储的,就是大端
return 0;
}
typedef union _un
{
char ch;
int i;
}
myunion;
int IsLittleEndian()
{
myunion un;
un.i = 1;//对i赋值,如果是小端 01 00 00 00,如果是大端 00 00 00 01
if(un.ch == 1) return 1;//小端
return 0;
}<br>//注意:最好是先给int赋值,然后判断char的值,在给int赋值后能够保证其他3个字节为0<br>int IsLittleEndian1() <br>{ <br> myunion un; <br> //un.i = 0;//清零<br> un.ch = 1;//对ch赋值,低地址字节一定是01,即:01 XX XX XX 除非初始化为0,那么为 01 00 00 00<br> if(un.i == 1) return 1;//小端 按照小端读出来是1<br> return 0; <br>}