Search code examples
c#arraysbitconvertersingle-precision

Byte[] to float conversion


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

Now my byte[] values are 82 184 126 63 .i.e.,

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.


Solution

  • This is working for me.

    float val = (float)0.995;
    Byte[] arr = BitConverter.GetBytes(val);
    
    float myFloat = BitConverter.ToSingle(arr, 0);