检测本地字节序 是大端存储还是小端存储

#include <stdio.h>

typedef union{
    unsigned short value;
    unsigned char byte[2];
}CodeOrderUnion;

void test_endian()
{
    CodeOrderUnion order;
    order.value = 0x0102;
    if(order.byte[0] == 0x01 && order.byte[1] == 0x02){
        printf("Small Endian\n");
    }
    else if(order.byte[0] == 0x02 && order.byte[1] == 0x01){
        printf("Big Endian\n");
    }
    else{
        printf("error!\n");
    }
}

int main()
{
    test_endian();
}

 

posted @ 2016-11-13 19:01  你好阿汤哥  Views(629)  Comments(0Edit  收藏  举报