首页 > 解决方案 > Byte[] 到浮点数的转换

问题描述

Float b = 0.995;
Byte[] a = Bitconverter.GetBytes(b);

现在我的byte[]值是 82 184 126 63 。即,

a[0] = 82, a[1] =184, a[2] = 126, and a[3] = 63.

我想恢复到字节以上浮动。所以,我用Bitconverter.Tosingle

Float b = Bitconverter.Tosingle(byte[] value,start index)

我的疑问是我需要赋予byte[]价值并开始索引。

您能否将解释的代码分享为代码。

标签: c#arraysbitconvertersingle-precision

解决方案


这对我有用。

float val = (float)0.995;
Byte[] arr = BitConverter.GetBytes(val);

float myFloat = BitConverter.ToSingle(arr, 0);

推荐阅读