Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66319
  • 博文数量: 19
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-13 16:29
文章分类

全部博文(19)

文章存档

2010年(9)

2009年(10)

我的朋友

分类: 服务器与存储

2010-10-06 22:30:43

Big endian and Little endian computers


Virtually all computer architectures are byte addressable. If an int is four bytes, there are two different ways to store this. Suppose the address of the int is A. In a so-called big endian computer, the highest order byte is stored at A, and the lowest order byte is stored at address A+3. In a so-called little endian computer, address A stores the least significant byte and the most significant byte is at address A+3.

Big endian computer architectures include the IBM 370, the Motorola 68000 and Sun Sparc. Little endian computers include the intel series (80486, pentium etc) and VAX.

Consider the decimal integer 91,329. This is 00 01 64 C1 in hexidecimal. If this were to be stored at address A in a big endian computer, 00 would be at address A, 01 at address A+1 64 at address A+2, and C1 at address A+3. On a little endian computer, C1 would be the value at address A, 64 at address A+1, 01 at address A+2, and 00 at address A+3.

Computer networks are big endian. This means that when little endian computers are going to pass integers over the network (IP addresses for example), they need to convert them to network byte order. Likewise, when the receive integer values over the network, they need to convert them back to their own native representation.

There are four functions that do this.


unsigned long htonl(unsigned long)
host to network conversion for long ints (4 bytes)
unsigned short htons(unsigned short)
host to network conversion for short ints (2 bytes)
unsigned long ntohl(unsigned long)
network to host conversion for long ints
unsigned short ntohs(short)
network to host conversion for short ints

On big endian computers these functions simply return their arguments.

The terms big endian and little endian are borrowed from Gulliver‘s Travels.

阅读(634) | 评论(0) | 转发(0) |
0

上一篇:strcpy()返回值问题

下一篇:没有了

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