C检测大小端模式
| int | |||
| 12 | 34 | 56 | 78 |
| 00001100 | 00100010 | 00111000 | 01001110 |
| 大端char | 小端char | ||
#include<stdio.h>
#include<stdlib.h>
int main()
{
union TestUnion
{
int a;
char b;
}tu;
tu.a= 0x12345678;
printf("%s\nb=0x%X\t a=0x%X\n",tu.b==0x12?"大端模式":"小端模式",tu.b,tu.a);
system("pause");
return 0;
}
例如一个16bit的short型x,在内存中的地址为0x0010,x的值为0x1122,那么0x11为高字节,0x22为低字节。对于 大端模式,就将0x11放在低地址中,即0x0010中,0x22放在高地址中,即0x0011中。小端模式,刚好相反。
浙公网安备 33010602011771号