原创 大端模式小端模式

2013-11-7 13:13 1322 9 9 分类: 工程师职场

以下程序是在同一台式机上vc6测试:

-----------------------------------------
//输出不同类型所占内存空间长度
#include "stdio.h"
int main()
{
 printf("char length:%d\n",sizeof(char));
 printf("short length:%d\n",sizeof(short));
 printf("int length:%d\n",sizeof(int)); 
 printf("long length:%d\n",sizeof(long));

 return 1;
}
//char length:1
//short length:2
//int length:4
//long length:4
//Press any key to continue
-----------------------------------------
//判断芯片的大端小端
#include "stdio.h"
int main()
{
 union w
 {
  int a; //4 bytes
  char b;//1 byte
 }c;

 c.a=1;
 if (c.b==1)
 {
  printf("It is Little_endian!\n");
 }
 else
 {
  printf("It is Big_endian!\n");
 }

 return 1;
}

//It is Little_endian!
//Press any key to continue
-----------------------------------------
//小端模式低地址存放最低位
#include "stdio.h"
int main()
{
 int aa = 0x12345678;

 printf("%x\n",aa);
 printf("%x\n",*(char *)(&aa));
 printf("%x\n",*((char *)(&aa)+1));
 printf("%x\n",*((char *)(&aa)+2));
 printf("%x\n",*((char *)(&aa)+3));

 return 1;
}
//12345678
//78
//56
//34
//12
//Press any key to continue
-----------------------------------------
//小端模式结构体中先定义的成员地址最低
#include "stdio.h"
int main()
{
 typedef struct __BigSmallEndian
 {
  char a;
  char b;
  char c;
  char d;
 }BigSmallEndian;
 
 BigSmallEndian Check;

 printf("Check Address:%x\n",&Check);
 printf("Check.a Address:%x\n",&(Check.a));
 printf("Check.b Address:%x\n",&(Check.b));
 printf("Check.c Address:%x\n",&(Check.c));
 printf("Check.d Address:%x\n",&(Check.d));

 return 1;
}
//Check Address:18ff44
//Check.a Address:18ff44
//Check.b Address:18ff45
//Check.c Address:18ff46
//Check.d Address:18ff47
//Press any key to continue
-----------------------------------------

#include "stdio.h"
int main(int argc, char *argv[])
{
 typedef struct __BigSmallEndian
 {
  char a;
  char b;
  short c;
  int d;
  char e;
 }BigSmallEndian;
 
 BigSmallEndian Check;

 printf("Check Address:%x\n",&Check);
 printf("Check.a Address:%x\n",&(Check.a));
 printf("Check.b Address:%x\n",&(Check.b));
 printf("Check.c Address:%x\n",&(Check.c));
 printf("Check.d Address:%x\n",&(Check.d));
 printf("Check.e Address:%x\n",&(Check.e));
}
//Check Address:18ff3c
//Check.a Address:18ff3c
//Check.b Address:18ff3d
//Check.c Address:18ff3e
//Check.d Address:18ff40
//Check.e Address:18ff44
//Press any key to continue
-----------------------------------------

#include "stdio.h"
int main(int argc, char *argv[])
{
 char array[5] = {'a','b',0,'A','B'};

 printf("arrar[0]Address:%x   ",&array[0]);printf("arrar[0] value:%x\n",array[0]);
 printf("arrar[1]Address:%x   ",&array[1]);printf("arrar[1] value:%x\n",array[1]);
 printf("arrar[2]Address:%x   ",&array[2]);printf("arrar[2] value:%x\n",array[2]);
 printf("arrar[3]Address:%x   ",&array[3]);printf("arrar[3] value:%x\n",array[3]);
 printf("arrar[4]Address:%x   ",&array[4]);printf("arrar[4] value:%x\n",array[4]);
}
//arrar[0]Address:18ff40   arrar[0] value:61
//arrar[1]Address:18ff41   arrar[1] value:62
//arrar[2]Address:18ff42   arrar[2] value:0
//arrar[3]Address:18ff43   arrar[3] value:41
//arrar[4]Address:18ff44   arrar[4] value:42
//Press any key to continue

文章评论0条评论)

登录后参与讨论
我要评论
0
9
关闭 站长推荐上一条 /2 下一条