判断你的计算机是 大端字节序 还是 小端字节序
#include <stdio.h>
union {
short value; // 2字节
char bytes[sizeof(short)]; // char[2]
} test;
int main(int argc, const char * argv[]) {
test.value = 0x0102;
if((test.bytes[0] == 1) && (test.bytes[1] == 2)) {
printf("大端字节序\n");
} else if((test.bytes[0] == 2) && (test.bytes[1] == 1)) {
printf("小端字节序\n");
} else {
printf("未知\n");
}
return 0;
}

浙公网安备 33010602011771号