1. 测试代码
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
int main(int agrc, char *argv[])
-
{
-
printf("short = %d, int = %d, long = %d\n", sizeof(short), sizeof(int), sizeof(long));
-
-
unsigned short test = 0x1234;
-
//unsigned long test = 0x12345678;
-
if(*( (unsigned char*) &test ) == 0x12)
-
printf("1: Big Endian\n");
-
else
-
printf("1: Little Endian\n");
-
-
static union {
-
char c[4];
-
unsigned int tmp;
-
} endian_test = { { 'l','?','?','b' } };
-
unsigned char uctmp = (char)endian_test.tmp;
-
-
if (uctmp == 'b') {
-
printf("2: Big Endian\n");
-
}
-
else if(uctmp == 'l') {
-
printf("2: Little Endian\n");
-
}
-
-
static union {
-
char c[4];
-
unsigned int tmp;
-
} endian_test1 = {0x12, 0x34, 0x56, 0x78};
-
-
printf("endian_test.tmp = 0x%08X\n", endian_test1.tmp);
-
printf("(char)endian_test.tmp = 0x%02X\n", (char)endian_test1.tmp);
-
if (endian_test1.tmp == 0x12345678) {
-
printf("3: Big Endian\n");
-
}
-
else if(endian_test1.tmp == 0x78563412) {
-
printf("3: Little Endian\n");
-
}
-
-
return 1;
-
-
}
2. 运行结果
虚拟机运行结果如下:
xxg@xxg-desktop:~/1-wire/hmac_sha1$ ./hello
short = 2, int = 4, long = 4
1: Little Endian
2: Little Endian
endian_test.tmp = 0x78563412
(char)endian_test.tmp = 0x12
3: Little Endian
openwrt下运行结果如下:
root@OpenWrt:/xutest# ./hello
short = 2, int = 4, long = 4
1: Big Endian
2: Big Endian
endian_test.tmp = 0x12345678
(char)endian_test.tmp = 0x78
3: Big Endian
阅读(3921) | 评论(0) | 转发(0) |