使用共用体测试大小端
typedef union bigSmallEnd{ unsigned short a; unsigned char b[2]; }BSE; void testBSE() { BSE test; test.a = 0x0102; if (test.b[0] == (unsigned char) 0x02) { printf("小端\n"); } else { printf("大端\n"); } } int main(){ testBSE(); return 0; }
typedef union bigSmallEnd{ unsigned short a; unsigned char b[2]; }BSE; void testBSE() { BSE test; test.a = 0x0102; if (test.b[0] == (unsigned char) 0x02) { printf("小端\n"); } else { printf("大端\n"); } } int main(){ testBSE(); return 0; }