使用共用体判断字节序
#include <myhead.h>
typedef union{
unsigned short int value;
unsigned char bytes[2];
}Bytes;
int main(int argc, const char *argv[])
{
unsigned short int val = 0x0201;
Bytes b;
b.value = val;
if(b.bytes[0] == 0x01 && b.bytes[1] == 0x02){
printf("小端字节序\n");
}else if(b.bytes[0] == 0x02 && b.bytes[1] == 0x01){
printf("大端字节序\n");
}
return 0;
}