我可以安全地假设 Windows 安装将始终是小端吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6449468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I safely assume that Windows installations will always be little-endian?
提问by jgottula
I'm writing a userspace filesystem driver on Windows and endianness conversions are something I've been dealing with, as this particular filesystem always stores values in little-endian format and the driver is expected to convert them (if necessary) for the CPU it's running on. However, I find myself wondering if I even need to worry about endianness conversions, since as far as I can tell, desktop Windows only supports little-endian architectures (IA32, x86-84, etc.), and therefore, the on-disk little-endian values are perfectly fine sans conversion. Is this observation accurate, and if so, is it generally acceptable to make the assumption that Windows will always be running on little-endian hardware? Additionally, is it even possible (in 2011) to run Windows on a big-endian emulator or something, such that one could even testfor endianness issues?
我正在 Windows 上编写用户空间文件系统驱动程序,并且字节序转换是我一直在处理的事情,因为这个特定的文件系统总是以小端格式存储值,并且驱动程序应该为 CPU 转换它们(如果需要)它是继续运行。但是,我发现自己想知道我是否需要担心字节序转换,因为据我所知,桌面 Windows 仅支持小字节序架构(IA32、x86-84 等),因此,磁盘上的little-endian 值是完美的无转换。这个观察是否准确,如果是,那么假设 Windows 将始终在小端硬件上运行通常是否可以接受?此外,甚至有可能(在 2011 年)在大端模拟器或其他东西上运行 Windows,这样人们甚至可以测试字节序问题?
Edit:For additional clarity, the way my code currently works, I do an endianness check at startup time, and then every time I load a value off the disk, I run it through an inline function that uses an intrinsic to change endianness if the architecture is big-endian. The problem is, I don't know if I might have missed one or more of those places where I needed to do a conversion and the easiest way to see if I screwed up is to run the program on a big-endian architecture. So I'm interested in knowing (a)if it's even necessary to do these checks since Windows doesn't ordinarily run on little-endian platforms (today anyway), and (b)how I could possibly test my code, seeing as I can't think of a way to run Windows on a big-endian architecture, and manually reversing allthe multibyte values on disk still involves a manual process that I might well screw up.
编辑:为了更清楚,我的代码当前的工作方式,我在启动时进行字节序检查,然后每次我从磁盘加载一个值时,我通过一个内联函数运行它,该函数使用内在的改变字节序,如果架构是大端的。问题是,我不知道我是否可能错过了一个或多个需要进行转换的地方,而查看我是否搞砸的最简单方法是在大端架构上运行程序。所以我很想知道(a)是否有必要进行这些检查,因为 Windows 通常不在小端平台上运行(无论如何今天),以及(b)我如何可能测试我的代码,就像我一样想不出在大端架构上运行 Windows 的方法,磁盘上的所有多字节值仍然涉及一个我很可能会搞砸的手动过程。
回答by Ana Betts
All versions of Windows that you'll see are little-endian, yes. The NT kernel actually runs on a big-endian architecture even today.
您将看到的所有 Windows 版本都是小端的,是的。即使在今天,NT 内核实际上还是在大端架构上运行。
回答by NtscCobalt
Edit after changed question:
更改问题后编辑:
A)No it is not necessary to check endianness if your sole target is Windows x86 or x64. I wouldn't even spend the time checking the endianness in that case.
A)不,如果您的唯一目标是 Windows x86 或 x64,则没有必要检查字节序。在这种情况下,我什至不会花时间检查字节序。
B)If you want to check bi-endian support of your code I recommend splitting it into libraries that are themselves cross platform compilable. Then compile and run the code on your favorite Linux flavor that supports big-endian and see if it works. I have yet to hear of any compiler or software that can detect bi-endian issues.
B)如果您想检查代码的双端支持,我建议将其拆分为本身可跨平台编译的库。然后在你最喜欢的支持大端的 Linux 风格上编译和运行代码,看看它是否有效。我还没有听说过任何可以检测双端问题的编译器或软件。
Original response:
原回复:
As far as I'm aware there are no desktop or server versions of windows that support big-endian. Itanium processors (which I believe were always called IA 64, not IA32 but I could be wrong) have the ability to run in big-endian but Windows doesn't support it.
据我所知,没有支持 big-endian 的桌面或服务器版本的 Windows。Itanium 处理器(我相信它一直被称为 IA 64,而不是 IA32,但我可能是错的)能够以大端字节序运行,但 Windows 不支持它。
This isn't to say that Windows 8 will be little-endian only as Windows 8 is targeting ARM processors.
这并不是说 Windows 8 将是小端的,因为 Windows 8 的目标是 ARM 处理器。
If for some reason you are on Windows (#ifdef _WIN32) and big-endian simply reverse the data structures when you load from disk and just always save in little-endian format which is much more common.
如果由于某种原因您使用的是 Windows (#ifdef _WIN32) 并且 big-endian 在您从磁盘加载时简单地反转数据结构,并且总是以更常见的小端格式保存。