Little_endian和Big_endian的区别和C测试程序

这里简单的进行描述一下也加深记忆
Little_endian:不管在内存和磁盘中存储都是内存的低地址存储数值的低位数
Big_endian:不管在内存和磁盘中存储都是内存的低址存储数值的高位数

注意这里是数值,字符串没有这样的问题。
比如626C 低位数就是6C高位数就是62

比如我们要存储
0x626c

内存地址
低---------------->高 
Little_endian
6C  62
Big_endian
62  6C


我们用如下的程序进行验证

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include <string.h>
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. using namespace std;

  6. bool check(void)
  7. {
  8.         char* test = new char[2];
  9.         strcpy(test,"lb");
  10.         short *m=(short *)test;


  11.         printf("%s is test srting %c's ascii is %x,%c's ascii is %x\n",test,test[0],test[0],test[1],test[1]);
  12.         printf("short number is %x\n",*m);


  13.         if(*m == 0x626c)
  14.         {
  15.                 return false;
  16.         }
  17.         else if(*m == 0x6c62)
  18.         {
  19.                 return true;
  20.         }
  21.         else
  22.         {
  23.                 printf("UNKOWN ERROR!\n");
  24.                 exit(1);
  25.         }
  26.         delete [] test; 
  27. }


  28. int main(void)
  29. {
  30.         if(check())
  31.         {
  32.                 printf("Big_endian\n");
  33.         }
  34.         printf("Little_endian\n");
  35. return 0;
  36. }


输出为:
lb is test srting l's ascii is 6c,b's ascii is 62
short number is 626c
Little_endian
我是LINUX下面跑的,用WINDOWS跑也是一样的。
LINUX和WINDOWS都是Little_endian,占时没在AIX这样的Big_endian测试


我们可以到我们的字符串输出任然是lb,l 的ascii是 6c,b的ascii是 62 ,但是当转换为数字short int后
显示为626c,那么我们可以确认他为小端Little_endian。

可以参考
http://blog.163.com/yuyi_vc/blog/static/170371753201182511825105/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7728585/viewspace-2124159/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7728585/viewspace-2124159/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值