我有这4个字节:0x41 0xCC 0xB7 0xCF 我必须找到号码25.5897503。
使用Windev,示例使用Transfer()函数,但我在C#
中找不到等效函数你能帮我一些指示吗?
由于
答案 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);