C/C++ 判断主机字节存储序列
1 #include<stdio.h> 2 void check_endian() 3 { 4 union 5 { 6 short value; 7 char union_bytes[sizeof(short)]; 8 }tester; 9 tester.value = 0x0102; 10 if( (tester.union_bytes[0] == 1) && (tester.union_bytes[1] == 2)) 11 { 12 printf("big endian\n"); 13 } 14 else if( (tester.union_bytes[0] == 2) && (tester.union_bytes[1] == 1) ) 15 { 16 printf("small endian\n"); 17 } 18 else 19 { 20 printf("unknown\n"); 21 } 22 } 23 int main(int argc,char *argv[]) 24 { 25 check_endian(); 26 return 0; 27 }