此文章只是留给为留给自己一个记忆而已,技术性不大。

1。概念
Big Endian : The most significant byte is on the left end of the word
Little Endian : The most significant byte is on the right end of the word
例如:word 0x1234 在内存中是
Big Endian : 低字节(left end of the word )0x12
                      高字节(right end of the word )0x34
End Endian : 低字节(left end of the word )0x34
                      高字节(right end of the word )0x12

2。其需要注意得地方
由于不同cpu对字节序的处理可能是不同,比如x86系列是使用little-endian字节序,SGI机器则是使用big-endian字节序。所以在对跨平台通讯的时候特别要注意字节序的转换。由于历史问题,网络字节序统一为big-endian。只要通讯双方机器在网络传输中统一把字节转换为big-endian,则不会出现问题。由此你可以了解到socket编程中系统为用户提供的字节序转换函数ntohl, htonl, ntohs, htons的含义所在了。

3。总结
假设我们要在不同byte ordering的机器之间传输和交换数据,那该怎么办呢,有两个方法,一是全部转换成文本来传输(如XML使用的),另一个方法两方都按照某一方的byte order,这时就涉及到了不同byte order之间相互转换的问题(网络传输标准如TCP/IP采用第二种方法并且由于历史的原因,byte ordering是big-endian的)。









posted on 2005-12-14 22:37 Jeffrey.Coding 阅读(324) 评论(0)  编辑 收藏 引用 所属分类: 基础与概念性的文章

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理