字节序理论和概念 [英] endianness theory and concept

查看:86
本文介绍了字节序理论和概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是任何编程语言都存在的问题。假设您在big-endian机器上编写了一些文件,并且您知道这一点。如果两个单字节值背对背写入,您怎么知道? Big-endian反转了16位,32位和64位值的顺序,那么您怎么知道需要将其读取为单个字节?

This isn't a question specific to any programming language. Say you have some file written on a big-endian machine, and you know this. If two single-byte values were written back-to-back, how would you know? Big-endian reverses the order of 16, 32, and 64 bit values, so how would you know you need to read it as individual bytes?

例如,您写字节0x11,然后是字节0x22。该文件然后包含0x1122。如果您是在小端机器上阅读的,则必须对其进行转换。那么您会读为2211还是1122吗?你知道吗?

For instance, you write the byte 0x11, then the byte 0x22. The file then contains 0x1122. If you read that on a little endian machine, you'd have to convert it. So would you read it as 2211, or 1122? Would you know how?

这有意义吗?我觉得这里缺少了一些超级基础的东西。

Does this make any sense? I feel like I'm missing something super basic here.

推荐答案

没有办法知道。这就是为什么正式指定的文件格式通常要求字节序,或者它们提供了一个选项(如Unicode,如MSN所述)的原因。这样,如果您正在读取具有特定格式的文件,则您已经知道它是big-endian格式的,因为该格式的事实意味着它具有特定的字节序。

There is no way to know. This is why formally specified file formats typically mandate an endianness, or they provide an option (as with unicode, as MSN mentioned). This way, if you are reading a file with a particular format, you know it's big-endian already, because the fact that it's in that format implies a particular endianness.

另一个很好的例子是网络字节顺序-网络协议通常是big-endian,因此,如果您是与Internet通讯的little-endian处理器,则必须向后写东西。如果您是高位优先的人,则无需担心。人们使用 htonl和ntohl 之类的功能来预处理他们写到网络,以便它们的源代码在所有计算机上都相同。这些函数被定义为在大字节序计算机上不执行任何操作,但在小字节序计算机上翻转字节。

Another good example of this is network byte order -- network protocols are typically big-endian, so if you're a little-endian processor talking to the internet, you have to write things backwards. If you're big-endian, you don't need to worry about it. People use functions like htonl and ntohl to preprocess things they write to the network so that their source code is the same on all machines. These functions are defined to do nothing on big-endian machines, but they flip bytes on little-endian machines.

关键的认识是,字节序是特定性的一个属性。 体系结构代表单词。他们不必以某种方式编写文件,这不是强制性的;它只是告诉您,体系结构上的指令期望多字节字以某种方式对其字节进行排序。大字节序的机器可以写入与小字节序的机器相同的字节序列,因为它必须对字节进行重新排序,因此它可能会使用更多指令来执行。对于编写大端格式的小端机器也是如此。

The key realization is that endianness is a property of how particular architectures represent words. It's not a mandate that they have to write files a certain way; it just tells you that the instructions on the architecture expect multi-byte words to have their bytes ordered a certain way. A big-endian machine can write the same byte sequence as a little-endian machine, it just might use a few more instructions to do it, because it has to reorder the bytes. The same is true for little-endian machines writing big-endian formats.

这篇关于字节序理论和概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆