这里简单的进行描述一下也加深记忆
Little_endian:不管在内存和磁盘中存储都是内存的低地址存储数值的低位数
Big_endian:不管在内存和磁盘中存储都是内存的低址存储数值的高位数
注意这里是数值,字符串没有这样的问题。
比如626C 低位数就是6C高位数就是62
比如我们要存储
0x626c
内存地址
低---------------->高
Little_endian
6C 62
Big_endian
62 6C
我们用如下的程序进行验证
-
#include<iostream>
-
#include <string.h>
-
#include<stdlib.h>
-
#include<stdio.h>
-
using namespace std;
-
-
bool check(void)
-
{
-
char* test = new char[2];
-
strcpy(test,"lb");
-
short *m=(short *)test;
-
-
-
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]);
-
printf("short number is %x\n",*m);
-
-
-
if(*m == 0x626c)
-
{
-
return false;
-
}
-
else if(*m == 0x6c62)
-
{
-
return true;
-
}
-
else
-
{
-
printf("UNKOWN ERROR!\n");
-
exit(1);
-
}
-
delete [] test;
-
}
-
-
-
int main(void)
-
{
-
if(check())
-
{
-
printf("Big_endian\n");
-
}
-
printf("Little_endian\n");
-
return 0;
-
}
输出为:
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博客 ” ,链接:https://blog.itpub.net/7728585/viewspace-2124159/
,如需转载,请注明出处,否则将追究法律责任。