Chinaunix首页 | 论坛 | 博客
  • 博客访问: 314373
  • 博文数量: 199
  • 博客积分: 8610
  • 博客等级: 中将
  • 技术积分: 1975
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-04 19:16
文章分类

全部博文(199)

文章存档

2007年(23)

2006年(176)

我的朋友

分类: C/C++

2006-12-07 15:43:36

假设:a=0x12345678;

则大端字节序和小端字节序的存储如下图所示:

                 Big-Endian                               Little-Endian

0字节            12h                                       78h

1字节            34h                                       56h

2字节            56h                                       34h

3字节            78h                                       21h

你也可以用下面的程序测验你的机器是大端字节序还是小端字节序:

#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;
}

此前所转的一篇文章上的测试程序是错误的,以免误导大家,所以闲来无事我写了一小段程序来测试。这段程序已经在我的机器上测试过,环境是VC6。我的机器是Intel的X86系列,所以是little endian.

 

阅读(867) | 评论(3) | 转发(0) |
0

上一篇:桂开三度

下一篇:12.7 日跑步日记

给主人留下些什么吧!~~