判断是大端字节序还是小端字节序

判断是大端字节序还是小端字节序

union

{

short  s;

char   c[sizeof(short)];

} un;

un.s = 0x0102;

if (sizeof(short) == 2)

{

if (un.c[0] == 1 && un.c[1] == 2)

printf("big-endian\n");

else if (un.c[0] == 2 && un.c[1] == 1)

printf("little-endian\n");

else

printf("unknown\n");

}

else

printf("sizeof(short) = %d\n", sizeof(short));

原文

unpv1

posted @ 2013-04-06 01:09  浪里飞  阅读(300)  评论(2编辑  收藏  举报