计算机房大端小端,大端小端计算机

本文介绍了计算机中的字节序概念,包括大端序(BigEndian)和小端序(LittleEndian)的存储方式。举例说明了两种字节序在存储四字节整数时的区别,并指出大多数计算机网络采用大端序。对于小端计算机在网络传输中需要将数据转换为网络字节序,为此存在htonl、htons等函数进行字节序转换。在接收到网络数据时,再转换回本地字节序。
摘要由CSDN通过智能技术生成

Big endian and Little endian computersVirtually 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值