1

我有这 4 个字节:0x41 0xCC 0xB7 0xCF,我必须找到数字 25.5897503。

使用 Windev,示例使用 Transfer() 函数,但我在 C# 中找不到等效函数

你能帮我一些迹象吗?

谢谢

4

1 回答 1

4

似乎Single需要精度。所以ToSingleBitConverter类中使用方法:

byte[] array = new byte[] {0x41, 0xCC, 0xB7, 0xCF};
float value = BitConverter.ToSingle(array, 0);

不过要小心小/大端。如果它没有按预期工作,请先尝试反转数组:

byte[] array = new byte[] {0x41, 0xCC, 0xB7, 0xCF};
Array.Reverse(array);
float value = BitConverter.ToSingle(array, 0);

编辑:

或者,正如Dimitry Bychenko建议的那样,您也可以使用BitConverter.IsLittleEndian来检查转换器的字节顺序

byte[] array = new byte[] {0x41, 0xCC, 0xB7, 0xCF}; //array written in Big Endian
if (BitConverter.IsLittleEndian) //Reverse the array if it does not match with the BitConverter endianess
    Array.Reverse(array);
float value = BitConverter.ToSingle(array, 0);
于 2017-05-05T10:53:58.317 回答