大小端测试, 原来openwrt是大端存储模式(WR703N下测试)

3920阅读 0评论2013-07-31 qiushui_007
分类:LINUX

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

上一篇:HMAC SHA1调试, 在虚拟机和openwrt下运行结果不同?
下一篇:openwrt下安装curl, 占用近1M空间, 常用命令集锦