大端和小端字节序

来源:互联网 发布:c语言简易病毒 编辑:程序博客网 时间:2024/05/14 09:40

 大端和小端字节序的问题在网络中以及在不同的操作系统的兼容性中是一个比较大的问题。它关系到不同操作系统和网络传输是否能够保证数据的语义正确性。


    对于一个字节而言,大端和小端没有任何的区别,但是对于多个字节而言,就存在着显著的区别。这个区别我们可以很容易想到,如果提供了一个地址,比如0x37041200,需要读取这个地址的一个字,也就是4个字节的一个数据。那么是读取从0x37041200开始到0x37041300这样的一个数,还是读取从0x37041200开始到0x37041100存储的4个字节的数。为此就出现了不同公司的两种实现--一个就是大端,一个就是小端。


你也可以用下面的程序测验你的机器是大端字节序还是小端字节序:
----------------------------------------------------------
#include <stdio.h>
int IsLittleEndian()
{
unsigned int usData = 0x12345678;
unsigned char *pucData = (unsigned char*)&usData;
if(*pucData == 0x78)
     return 1;
else
     return 0;
}

int main(void)
{
    if(IsLittleEndian())
        printf("is little endian!\n");
    else
        printf("is big endian!\n");
    return 0;
}

我的机器是Intel的X86系列,所以是little endian.


a=0x12345678
----------------------------------------------------------
"Little Endian" means that the low-order byte of the number is stored in memory at the lowest address, and the high-order byte at the highest address. (The little end comes first.) For example, a 4 byte LongInt 

    Byte3 Byte2 Byte1 Byte0

will be arranged in memory as follows: 
    Base Address+0   Byte0     78h    
    Base Address+1   Byte1     56h
    Base Address+2   Byte2     34h
    Base Address+3   Byte3     12h

Intel processors (those used in PC's) use "Little Endian" byte order.


"Big Endian" means that the high-order byte of the number is stored in memory at the lowest address, and the low-order byte at the highest address. (The big end comes first.) Our LongInt, would then be stored as: 

    Base Address+0   Byte3    12h
    Base Address+1   Byte2      34h
    Base Address+2   Byte1      56h
    Base Address+3   Byte0      78h

Motorola processors (those used in Mac's) use "Big Endian" byte order 




a=0x12345678
----------------------------------------------------------
Little-Endian: Base Address decrease(递减)
                 Little-Endian
         |-----------|<--- Base Address
         |    78     |
         |-----------|
         |    56     |
         |-----------|
         |    34     |
         |-----------|
         |    12     |
         |-----------|



         |-----------|
         |    78     |
         |-----------|
         |    56     |
         |-----------|
         |    34     |
         |-----------|
         |    12     |
         |-----------|<--- Base Address
                Big-Endian
Big-Endian: Base Address increase(递增)

大端模式和小端模式的区别

嵌入式系统开发者应该对Little-endian和Big-endian模式非常了解。例如,16bit宽的数0x1234在Little-endian模式CPU内存中的存放方式(假设从地址0x4000开始存放)为:

内存地址

0x4000

0x4001

存放内容

0x34

0x12

而在Big-endian模式CPU内存中的存放方式则为:

内存地址

0x4000

0x4001

存放内容

0x12

0x34

有时候,用C语言写程序时需要知道是大端模式还是小端模式。 所谓的大端模式,是指数据的低位保存在内存的高地址中,而数据的高位,保存在内存的低地址中;所谓的小端模式,是指数据的低位保存在内存的低地址中,而数据的高位保存在内存的高地址中。为什么会有大小端模式之分呢?这是因为在计算机系统中,我们是以字节为单位的,每个地址单元都对应着一个字节,一个字节为8bit。但是在C语言中除了8bit的char之外,还有16bit的short型,32bit的long型(要看具体的编译器),另外,对于位数大于8位的处理器,例如16位或者32位的处理器,由于寄存器宽度大于一个字节,那么必然存在着一个如果将多个字节安排的问题。因此就导致了大端存储模式和小端存储模式。例如一个16bit的short型x,在内存中的地址为0x0010,x的值为0x1122,那么0x11为高字节,0x22为低字节。对于大端模式,就将0x11放在低地址中,即0x0010中,0x22放在高地址中,即0x0011中。小端模式,刚好相反。我们常用的X86结构是小端模式,而KEIL C51则为大端模式。很多的ARM,DSP都为小端模式。有些ARM处理器还可以由硬件来选择是大端模式还是小端模式。 下面这段代码可以用来测试一下你的编译器是大端模式还是小端模式: short int x;

char x0,x1;

x=0x1122;

x0=((char*)&x)[0]; //低地址单元
x1=((char*)&x)[1]; //高地址单元

若x0=0x11,则是大端; 若x0=0x22,则是小端......

int main()
{
short int x;

char x0,x1;

x=0*((char*)&x); //低地址单元 ,或者((char*)&x)[0];

x1=*((char*)&x + 1); //高地址单元,或者((char*)&x)[1];

printf("%x\n%x\n",x0,x1);
}

int checkCPU( )

    { 
           union w 
           {      int a; 
                  short b; 
           } c; 
            c.a = 0x12345678; 
           return(c.b == 0x5678);
    } 

int main()
{
    printf("%d\n",checkCPU());
}

0 0
原创粉丝点击