十六进制数 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16102765/
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
Hexadecimal numbers C#
提问by pinker
I am trying to read a binary file in C#, but I am facing a problem. I declared the following:
我正在尝试用 C# 读取二进制文件,但我遇到了问题。我声明如下:
public static readonly UInt32 NUMBER = 0XCAFEBABE;
Then while reading from the very beginning of the file I am asking to read the first 4 bytes (already tried different ways, but this is the simplest):
然后在从文件的开头读取时,我要求读取前 4 个字节(已经尝试了不同的方法,但这是最简单的):
UInt32 num = in_.ReadUInt32(); // in_ is a BinaryReader
While I have that the 4 bytes are CA, FE, BA and BE (in hex) while convert them to UIntI am getting different values. NUMBER is 3405691582, num is 3199925962.
I also tried to do this:
虽然我知道 4 个字节是 CA、FE、BA 和 BE(以十六进制表示),但将它们转换为UInt我得到了不同的值。NUMBER 是 3405691582,num 是 3199925962。我也试过这样做:
byte[] f2 = {0xCA, 0xFE, 0xBA, 0xBE};
and the result of doing BitConverter.ToUInt32(new byte[]{0xCA, 0xFE, 0xBA, 0xBE},0)is 3199925962.
执行的结果BitConverter.ToUInt32(new byte[]{0xCA, 0xFE, 0xBA, 0xBE},0)是 3199925962。
can anyone help me?
谁能帮我?
thanks
谢谢
回答by Thorarin
This is because of the little endiannessof your machine. See BitConverter.IsLittleEndianproperty to check this.
这是因为你的机器的小字节序。请参阅BitConverter.IsLittleEndian财产以检查这一点。
Basically, numbers are stored in reverse byte order, compared to how you would write them down. We write the most significant number on the left, but the (little endian) PC stores the leastsignificant byte on the left. Thus, the result you're getting is really 0xBEBAFECA(3199925962 decimal) and not what you expected.
基本上,与您写下它们的方式相比,数字以相反的字节顺序存储。我们在左侧写入最高有效数字,但(小端)PC在左侧存储最低有效字节。因此,您得到的结果确实是0xBEBAFECA(十进制 3199925962)而不是您所期望的。
You can convert using bit shifting operations:
您可以使用位移操作进行转换:
uint value = (f2[0] << 24) | (f2[1] << 16) | (f2[2] << 8) | f2[3];
There are many more ways to convert, including IPAddress.NetworkToHostOrderas I4V pointed out, f2.Reverse(), etc.
还有更多的转换方法,包括IPAddress.NetworkToHostOrderI4V 指出的f2.Reverse(),等等。
For your specific code, I believe this would be most practical:
对于您的特定代码,我相信这将是最实用的:
uint num = (uint)IPAddress.NetworkToHostOrder(in_.ReadInt32());
This may result in an arithmetic underflow however, so it may cause problems with a /checkedcompiler option or checkedkeyword(neither are very common).
然而,这可能会导致算术下溢,因此可能会导致/checked编译器选项或checked关键字出现问题(两者都不是很常见)。
If you want to deal with these situations and get even cleaner code, wrap it in an extension method:
如果您想处理这些情况并获得更清晰的代码,请将其包装在扩展方法中:
public static uint ReadUInt32NetworkOrder(this BinaryReader reader)
{
unchecked
{
return (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32());
}
}
回答by Hossein Narimani Rad
That's what is called byte order:
这就是所谓的字节顺序:
var result1 = BitConverter.ToUInt32(new byte[] { 0xCA, 0xFE, 0xBA, 0xBE }, 0);
//3199925962
var result2 = BitConverter.ToUInt32(new byte[] { 0xBE, 0xBA, 0xFE, 0xCA }, 0);
//3405691582

