converting byte to float

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aykut

    converting byte to float

    Hi
    I have to convert byte value to float in C#.Float is in IEEE 754 format.
    byte value is= 70 23 22 195 in a byte array.
    and its float value is = -150.0909
    what is the algoritm for this conversion?
    thanks for help


  • Morten Wennevik

    #2
    Re: converting byte to float

    Hi Aykut,

    You can use a MemoryStream to create a Stream of your byte data, then read a float (Single) from the stream using a BinaryReader.

    byte[] b = new byte[]{70, 23, 22, 195};
    float f = 0;
    using(MemoryStr eam ms = new MemoryStream(b) )
    {
    using(BinaryRea der br = new BinaryReader(ms ))
    {
    f = br.ReadSingle() ;
    }
    }

    --
    Happy coding!
    Morten Wennevik [C# MVP]

    Comment

    • MarkT [developmentor]

      #3
      RE: converting byte to float

      > byte value is= 70 23 22 195 in a byte array.[color=blue]
      > and its float value is = -150.0909
      > what is the algoritm for this conversion?[/color]

      I think System.BitConve rter might do what you want.

      public static float ToSingle(
      byte[] value,
      int startIndex
      );

      Comment

      • Aykut

        #4
        Re: converting byte to float

        Thanks for the replies .
        But I d rather like to know the algorithm behind it.
        if it isnt too complicated :)
        regards


        Comment

        Working...