Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1511197
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: LINUX

2013-07-31 10:33:26

1. 测试代码

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int main(int agrc, char *argv[])
  5. {
  6.     printf("short = %d, int = %d, long = %d\n", sizeof(short), sizeof(int), sizeof(long));

  7.     unsigned short test = 0x1234;
  8.     //unsigned long test = 0x12345678;
  9.     if(*( (unsigned char*) &test ) == 0x12)
  10.         printf("1: Big Endian\n");
  11.     else
  12.         printf("1: Little Endian\n");

  13.     static union {
  14.      char c[4];
  15.      unsigned int tmp;
  16.     } endian_test = { { 'l','?','?','b' } };
  17.     unsigned char uctmp = (char)endian_test.tmp;

  18.     if (uctmp == 'b') {
  19.         printf("2: Big Endian\n");
  20.     }
  21.     else if(uctmp == 'l') {
  22.         printf("2: Little Endian\n");
  23.     }

  24.     static union {
  25.      char c[4];
  26.      unsigned int tmp;
  27.     } endian_test1 = {0x12, 0x34, 0x56, 0x78};

  28.     printf("endian_test.tmp = 0x%08X\n", endian_test1.tmp);
  29.     printf("(char)endian_test.tmp = 0x%02X\n", (char)endian_test1.tmp);
  30.     if (endian_test1.tmp == 0x12345678) {
  31.         printf("3: Big Endian\n");
  32.     }
  33.     else if(endian_test1.tmp == 0x78563412) {
  34.         printf("3: Little Endian\n");
  35.     }
  36.     
  37.     return 1;
  38.     
  39. }


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) |
给主人留下些什么吧!~~