大小端存储模式

大端模式:低字节存储在高地址

小端模式:低字节存储在低地址

例如 int i = 1;占4个字节

大端模式:

低地址 ---- 0x0  0x0 0x0 0x1 ---高地址

小端模式:

高地址-----0x0 0x0 0x0 0x1 ----低地址


程序检测:

#include"stdio.h"
//方法1
void checkSysetem1()
{
	unsigned short usData = 0x1122;
	unsigned char  *pucData ; 
	pucData = (unsigned char*)&usData;//short两个字节,强制转换为char 1个字节 保留低地址
	if(*pucData == 0x22)
	{
		printf("小端");
	}
	else
	{
		printf("大端");
	}
}
//方法2
int checkSystem2()
{
	union check//利用union类型 所有成员的启始地址一致
	{
		int i;//4字节
		char ch;//1字节
	}c;
	c.i = 1;
	return(c.ch == 1);//启始地址存1,表明1在低地址存着
}

void main()
{
	checkSystem1();
	if(checkSystem())printf("小端");
	else printf("大端");
}


posted @ 2011-08-31 20:13  foreverlearn  阅读(144)  评论(0编辑  收藏  举报