其中.NET兼容的架构不小端?架构、NET

2023-09-03 05:51:21 作者:了與丗事

借助 BitConverter 类有一个字段的 IsLittleEndian 其中,根据文档:

The BitConverter class has a field IsLittleEndian which, according to the documentation:

表示,其中的数据存储在该计算机体系结构的字节顺序(字节序)。

Indicates the byte order ("endianness") in which data is stored in this computer architecture.

我注意到反射该字段是硬codeD为true BitConverter的静态构造函数。

I notice in Reflector that the field is hard-coded to true in the static constructor of BitConverter.

所以,我的问题是,是否需要使用BitConverter时,考虑到IsLittleEndian的 - 换句话说,有没有对大端平台上运行的任何.NET实现?如果没有,什么是该领域的摆在首位的目的是什么?

So my question is, do I need to take account of IsLittleEndian when using the BitConverter - in other words, are there any .Net implementations running on big-endian platforms? And if not, what was the purpose of the field in the first place?

推荐答案

在CLI标准不禁止任何特定的字节序,所以如果你想你的程序可移植,你应该的没有的依赖于特别是字节顺序......当然,除非在场景中的特定字节顺序是必需的,比如一些数据交换协议(感谢用户的MOOF指出了这一点)。

The CLI standard does not proscribe any particular Endianness, so if you want your program to be portable, you should not depend on a particular byte order... unless of course in scenarios where a particular byte ordering is required, such as with some data exchange protocols (thanks to user The Moof for pointing this out).

从CLI注释标准(第161页)—分区I卷,第12.6.3:字节顺序:

From the CLI Annotated Standard (p.161) — Partition I, section 12.6.3: "Byte Ordering":

有关数据类型超过1个字节时,字节顺序取决于目标CPU上。 code,它取决于字节顺序可能无法在所有的平台上运行。 [...] 的

我怀疑你的反射看到了一个硬$ C $光盘 IsLittleEndian 价值,因为你下载的时候/计算机上安装的.NET框架中,特定的安装包是针对特定的平台(如英特尔的x86,这是小端)。

I suspect that you saw a hard-coded value for IsLittleEndian in Reflector because when you downloaded/installed the .NET Framework on your machine, that particular installation package was targeted at a particular platform (e.g. Intel x86, which is Little Endian).

我可以这样想象,也有.NET framework的其他安装包已 IsLittleEndian 硬连接到返回不同的值,根据不同的平台,特别是安装在目标。

I could thus imagine that there are other installation packages of the .NET framework that have IsLittleEndian hard-wired to return a different value, depending on the platform that particular installation targets.