IsLittleEndian场报告假的,但它必须是小端? [英] IsLittleEndian field reports false, but it must be Little-Endian?

查看:285
本文介绍了IsLittleEndian场报告假的,但它必须是小端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是个电脑的英特尔(Win7的64位)上运行,并根据我读的英特尔是用小端。我尝试了这一点在C#中使用下面的代码:

Im running on a Intel computer (Win7 64-bit) and according to what I read Intel is using Little-Endian. I try this out in C# with the following code:

byte[] b2 = new byte[] { 0, 1 };
short b2short = BitConverter.ToInt16(b2, 0);

和b2short == 256从一个小端的预期。

and b2short == 256 as expected from a Little-Endian.

然后我读,在.NET中,BitConverter.IsLittleEndian应反映端系统使用的是,当我在Visual Studio中检查变量它报告的的,也就是说,它不是小端。

Then I read that in .NET, the BitConverter.IsLittleEndian should reflect what endian the system is using, and when I check the variable in Visual Studio it reports false, ie it is NOT Little-Endian.

这是否有什么与64位操作系统?任何想法

Does that have anything to do with 64-bit OS? Any ideas?

编辑:
我collegue,谁从我对面坐下,做了同样的测试(赢Vista 32位),并得到了相同的结果


My collegue, who sits across from me, did the same test (Win Vista 32-bit) and got the same results


编辑2:
这真是奇怪。每当我运行代码,并打破BitConverter做的事,IsLittleEndian ==假后。
,但如果我添加行 Console.WriteLine(BitConverter.IsLittleEndian); 的事后为TRUE:

EDIT 2: This is REALLY strange. Whenever I run the code, and break after the BitConverter did its thing, the IsLittleEndian == false. BUT, if I add the line Console.WriteLine(BitConverter.IsLittleEndian); afterwards it is TRUE:

byte[] b2 = new byte[] { 0, 1 };
short b2short = BitConverter.ToInt16(b2, 0);
Console.WriteLine(BitConverter.IsLittleEndian);
// Now the IsLittleEndian is true



但是,一旦我删除Console.WriteLine命令是试。

我也可以添加,即使我对Console.WriteLine的IsLittleEndian突破==真实的,但如果我。完全消除行那是假的。

I can also add that even if I break on the "Console.WriteLine" the IsLittleEndian == true, but if I remove the line altogether it is false.

编辑3:
正如马克Gravell指出,这一定是一些时序错误。如果我使用的变量BitConverter.IsLittleEndian它被初始化,如果我不(和断裂的时候看就可以了)它没有初始化,因此 ...

推荐答案

我不知道这是一个计时错误,或许与beforefieldinit......你是如何看的价值?这可能是该类型初始化(为 BitConverter )是没有得到由VS调试器触发(这是在幕后偷看,可以这么说)。特别是因为是一个字段的默认值...

I wonder if this is a timing bug, perhaps related to "beforefieldinit"... how are you looking at the value? It is possible that the type-initializer (for BitConverter) isn't getting triggered by the VS debugger (which is peeking under the covers, so to speak). Especially since false is the default value for a field...

IsLittleEndian 静态字段设置在静态构造函数;而一个初始化执行时间是......很难预测。如果您在使用调试器,全盘皆输。可靠地检查这个字段的值的唯一方法是通过代码(当需要它面前的CLR将在某个时候运行初始化):

The IsLittleEndian static field is set in the static constructor; and the time that an initializer executes is... very hard to predict. If you are using a debugger, all bets are off. The only way to reliably check the value of this field is via code (when the CLR will run the initializer at some point before it is required):

bool isLittleEndian = BitConverter.IsLittleEndian;



不要相信调试器/监视窗口等。

Don't trust the debugger / watch windows etc.

这篇关于IsLittleEndian场报告假的,但它必须是小端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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