使用共用体测试大小端

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

 

posted @ 2020-03-04 10:32  路璐  阅读(271)  评论(0编辑  收藏  举报