判断你的计算机是 大端字节序 还是 小端字节序

#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;
}

 

posted @ 2022-06-05 22:43  OXYGEN1  阅读(24)  评论(0)    收藏  举报