两种很简单的判断系统是大端还是小端模式
时间:2010-09-03 来源:tianchunlong
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
用联合体来判断:
#include<stdio.h>
union { unsigned short a; char b; }fan;
void main(void) { fan.a=0x2211; printf("fan.b=%x\n",fan.b); } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fan@fan:~/arm$ gcc -o app little.c fan@fan:~/arm$ ./app fan.b=11//低位对应低地址,所以为小端 fan@fan:~/arm$ ====================================================================== +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 使用指针强转换 #include<stdio.h>
void main(void) { unsigned short a; a=0x2211; printf("(char)a[0]=%x\n",((char*)&a)[0]);//注意他们的优先级 printf("(char)a[1]=%x\n",((char*)&a)[1]); } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 运行结果: fan@fan:~/arm$ ./app (char)a[0]=11//和上面的结果一样,也是小端模式 (char)a[1]=22 fan@fan:~/arm$
union { unsigned short a; char b; }fan;
void main(void) { fan.a=0x2211; printf("fan.b=%x\n",fan.b); } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fan@fan:~/arm$ gcc -o app little.c fan@fan:~/arm$ ./app fan.b=11//低位对应低地址,所以为小端 fan@fan:~/arm$ ====================================================================== +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 使用指针强转换 #include<stdio.h>
void main(void) { unsigned short a; a=0x2211; printf("(char)a[0]=%x\n",((char*)&a)[0]);//注意他们的优先级 printf("(char)a[1]=%x\n",((char*)&a)[1]); } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 运行结果: fan@fan:~/arm$ ./app (char)a[0]=11//和上面的结果一样,也是小端模式 (char)a[1]=22 fan@fan:~/arm$
相关阅读 更多 +