简体   繁体   中英

Byte[] to float conversion

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

Now my byte[] values are 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

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

My doubt is what I need to give byte[] value and start index.

Can you pls share as a code along explanation.

value is just the byte array that holds the float.
The startIndex means the offset with which the conversion function will start reading the 4 bytes that make a float from the passed array. In your case it should just be 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)

Parameters

  • value
    Byte[]
    An array of bytes.
  • startIndex
    Int32
    The starting position within value .

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:

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:

       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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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