简体   繁体   English

Byte[] 到浮点数的转换

[英]Byte[] to float conversion

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

Now my byte[] values are 82 184 126 63.ie,现在我的byte[]值为 82 184 126 63.ie,

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

I want to revert back above byte to float.So,I used Bitconverter.Tosingle我想将字节恢复为浮点数。所以,我使用Bitconverter.Tosingle

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

My doubt is what I need to give byte[] value and start index.我的疑问是我需要给byte[]值和开始索引。

Can you pls share as a code along explanation.您能否将解释的代码分享为代码。

value is just the byte array that holds the float. value只是保存浮点数的字节数组。
The startIndex means the offset with which the conversion function will start reading the 4 bytes that make a float from the passed array. startIndex表示转换 function 将开始读取从传递的数组中生成浮点数的 4 个字节的偏移量。 In your case it should just be 0.在你的情况下,它应该只是 0。

This is working for me.这对我有用。

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

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

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

Parameters参数

  • value价值
    Byte[]字节[]
    An array of bytes.一个字节数组。
  • startIndex开始索引
    Int32整数32
    The starting position within value .起始值position 以内。

The array you get is only 4 bytes long, you need 4 bytes to create the single so position should be 0 - all others give you exceptions:你得到的数组只有 4 个字节长,你需要 4 个字节来创建单个所以 position 应该是 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);
            }
        }
    }
}

Output: Output:

       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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM