【问题标题】:Byte[] to float conversionByte[] 到浮点数的转换
【发布时间】:2020-10-09 08:44:17
【问题描述】:
Float b = 0.995;
Byte[] a = Bitconverter.GetBytes(b);

现在我的 byte[] 值是 82 184 126 63 .i.e.,

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

我想将上面的字节恢复为浮点数。所以,我使用了Bitconverter.Tosingle

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

我的疑问是我需要给byte[] 值和起始索引。

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

【问题讨论】:

  • floating 标签与 CSS 相关...你能给出更合适的标签吗,比如你使用的语言?

标签: c# arrays bitconverter single-precision


【解决方案1】:

这对我有用。

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

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

【讨论】:

  • (float)0.995 的另一种写法是0.995f
【解决方案2】:

value 只是保存浮点数的字节数组。
startIndex 表示转换函数将开始读取从传递的数组中生成浮点数的 4 个字节的偏移量。在您的情况下,它应该只是 0。

【讨论】:

    【解决方案3】:

    BitConverter.ToSingle(byte[] value, int startIndex)

    参数

    • 价值
      字节[]
      一个字节数组。
    • 开始索引
      Int32
      内的起始位置。

    你得到的数组只有 4 个字节长,你需要 4 个字节来创建单个所以位置应该是 0 - 所有其他的都给你例外:

    using System;
    
    public class Program
    {
        public static void Main()
        {
            var b = 0.995f;
            Byte[] a = BitConverter.GetBytes(b);
            Console.WriteLine("{0,16:f7}{1,20}\n", b, BitConverter.ToString(a));
            for (var pos = 0; pos < a.Length; pos++)
            {
                try {
                    var c = BitConverter.ToSingle(a, pos);
                    Console.WriteLine("{0} is valid:",pos);
                    Console.WriteLine("{0}\n",c);
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} is invalid: {1}",pos,e);
                }
            }
        }
    }
    

    输出:

           0.9950000         52-B8-7E-3F
    
    0 is valid:
    0.995
    
    1 is invalid: System.ArgumentException: Destination array is not long enough to copy all the 
                  items in the collection. Check array index and length.
       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.BitConverter.ToSingle(Byte[] value, Int32 startIndex)
       at Program.Main() in d:\Windows\Temp\cowicrki.0.cs:line 13
    2 is invalid: System.ArgumentException: Destination array is not long enough to copy all the 
                  items in the collection. Check array index and length.
       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.BitConverter.ToSingle(Byte[] value, Int32 startIndex)
       at Program.Main() in d:\Windows\Temp\cowicrki.0.cs:line 13
    3 is invalid: System.ArgumentException: Destination array is not long enough to copy all the 
                  items in the collection. Check array index and length.
       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.BitConverter.ToSingle(Byte[] value, Int32 startIndex)
       at Program.Main() in d:\Windows\Temp\cowicrki.0.cs:line 13
    

    【讨论】:

    • 之前没注意过BitConverter.ToString(byte[])这个方法;很好。
    • @JeppeStigNielsen 如果你想从字节到字符串,你应该使用System.Text.Encoding.Default.GetString( byte[] )
    • @Sealad 不,字节数组{ 0x52, 0xB8, 0x7E, 0x3F, }代表一个单精度浮点类型的值;它不代表文本字符串。我看不出这种方法有什么意义。通常我会做类似string.Join("-", a.Select(val =&gt; $"{val:X2}")) 的事情来获得数组a 的内容的表示。但是BitConverter.ToString 方法已经产生了这种表示。
    猜你喜欢
    • 2011-06-03
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    相关资源
    最近更新 更多