4个字节到十进制 - C#来自Windev

时间:2017-05-05 10:51:50

标签: c# windev

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

使用Windev,示例使用Transfer()函数,但我在C#

中找不到等效函数

你能帮我一些指示吗?

由于

1 个答案:

答案 0 :(得分:4)

似乎需要Single精度。因此,请在ToSingle类中使用BitConverter方法:

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

谨防Little / Big Endian。如果它没有按预期工作,请尝试首先反转数组:

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

修改

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

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);