From byte[] to float and back again

Discussion in 'Microsoft C# .NET' started by Guest, Feb 22, 2005.

  1. Guest

    Guest Guest

    I do not know how to convert from a byte array to a float, and back again. I
    read data from a serial port into a byte[] (entire command structure which I
    parse). I am able to sift the data and isolate the information I want, in
    this case a structure that contains a bunch of floats and longs.

    So, lets say I have a byte[] containing the 4 bytes of info that are
    actually a float. In old C code, I would simple declare a pointer to the
    correct location in the array and call it a pointer to a float. Make sense?
    How do I do this with C#? I know how to go between a float and a text box,
    that's simple. But how to switch between the byte array and a float? I see
    how to do a single byte, but not a byte array. This same problem involves
    converting between longs and byte[].

    I hope there is a simple way to do this. With pointers and C, it's a piece
    of cake. Just need a little help with C#.
     
    Guest, Feb 22, 2005
    #1
    1. Advertisements

  2. Hi Kenny,

    There might be much easier ways, but you could feed the byte[] into a
    MemoryStream and use a BinaryReader.ReadSingle on the stream. Haven't
    tested it though.

    Another solution might be to use an unsafe code block and do it with
    pointers.

    --
    Happy Coding!
    Morten Wennevik [C# MVP]
     
    Morten Wennevik, Feb 22, 2005
    #2
    1. Advertisements

  3. You could use System.BitConverter.ToSingle.


    You can also do it by yourself:
    // Assume the byte array is the variable 'data'.
    private unsafe float ByteArrayToSingle ( byte [] buffer )
    {
    fixed (byte * p = buffer)
    return * ((float *) p);
    }
    You will then have to compile with the /unsafe switch.




    --
    Regards,
    Dennis JD Myrén
    Oslo Kodebureau
    "Kenny ODell" <> wrote in message
    news:...
    >I do not know how to convert from a byte array to a float, and back again.
    >I
    > read data from a serial port into a byte[] (entire command structure which
    > I
    > parse). I am able to sift the data and isolate the information I want, in
    > this case a structure that contains a bunch of floats and longs.
    >
    > So, lets say I have a byte[] containing the 4 bytes of info that are
    > actually a float. In old C code, I would simple declare a pointer to the
    > correct location in the array and call it a pointer to a float. Make
    > sense?
    > How do I do this with C#? I know how to go between a float and a text
    > box,
    > that's simple. But how to switch between the byte array and a float? I
    > see
    > how to do a single byte, but not a byte array. This same problem involves
    > converting between longs and byte[].
    >
    > I hope there is a simple way to do this. With pointers and C, it's a
    > piece
    > of cake. Just need a little help with C#.
     
    Dennis Myrén, Feb 22, 2005
    #3
  4. Guest

    Guest Guest

    Thanks for the hint. I don't know what a MemoryStream is, or how to do what
    you propose, so if you can provide more details, great. Bear with me. I'm
    an embedded C guy (some assembly). C# is a world away from what I normally
    work with. :)

    "Morten Wennevik" wrote:

    > Hi Kenny,
    >
    > There might be much easier ways, but you could feed the byte[] into a
    > MemoryStream and use a BinaryReader.ReadSingle on the stream. Haven't
    > tested it though.
    >
    > Another solution might be to use an unsafe code block and do it with
    > pointers.
    >
    > --
    > Happy Coding!
    > Morten Wennevik [C# MVP]
    >
     
    Guest, Feb 22, 2005
    #4
  5. Kenny ODell <> wrote:
    > I do not know how to convert from a byte array to a float, and back again. I
    > read data from a serial port into a byte[] (entire command structure which I
    > parse). I am able to sift the data and isolate the information I want, in
    > this case a structure that contains a bunch of floats and longs.
    >
    > So, lets say I have a byte[] containing the 4 bytes of info that are
    > actually a float. In old C code, I would simple declare a pointer to the
    > correct location in the array and call it a pointer to a float. Make sense?
    > How do I do this with C#? I know how to go between a float and a text box,
    > that's simple. But how to switch between the byte array and a float? I see
    > how to do a single byte, but not a byte array. This same problem involves
    > converting between longs and byte[].
    >
    > I hope there is a simple way to do this. With pointers and C, it's a piece
    > of cake. Just need a little help with C#.


    Use BitConverter - and if you need to work with both big and little
    endianess, I have a library which would help you:

    http://www.pobox.com/~skeet/csharp/miscutil

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Feb 22, 2005
    #5
  6. Guest

    Guest Guest

    Thanks so much. The bit converter did the trick for going from byte[] to
    float. How about the other way? I now need to take a float and put it in a
    byte[] so that I can send it back to the embedded target. How do I grab the
    data?

    "Dennis Myrén" wrote:

    > You could use System.BitConverter.ToSingle.
    >
    >
    > You can also do it by yourself:
    > // Assume the byte array is the variable 'data'.
    > private unsafe float ByteArrayToSingle ( byte [] buffer )
    > {
    > fixed (byte * p = buffer)
    > return * ((float *) p);
    > }
    > You will then have to compile with the /unsafe switch.
    >
    >
    >
    >
    > --
    > Regards,
    > Dennis JD Myrén
    > Oslo Kodebureau
    > "Kenny ODell" <> wrote in message
    > news:...
    > >I do not know how to convert from a byte array to a float, and back again.
    > >I
    > > read data from a serial port into a byte[] (entire command structure which
    > > I
    > > parse). I am able to sift the data and isolate the information I want, in
    > > this case a structure that contains a bunch of floats and longs.
    > >
    > > So, lets say I have a byte[] containing the 4 bytes of info that are
    > > actually a float. In old C code, I would simple declare a pointer to the
    > > correct location in the array and call it a pointer to a float. Make
    > > sense?
    > > How do I do this with C#? I know how to go between a float and a text
    > > box,
    > > that's simple. But how to switch between the byte array and a float? I
    > > see
    > > how to do a single byte, but not a byte array. This same problem involves
    > > converting between longs and byte[].
    > >
    > > I hope there is a simple way to do this. With pointers and C, it's a
    > > piece
    > > of cake. Just need a little help with C#.

    >
    >
    >
     
    Guest, Feb 22, 2005
    #6
  7. Kenny ODell <> wrote:
    > Thanks so much. The bit converter did the trick for going from byte[] to
    > float. How about the other way? I now need to take a float and put it in a
    > byte[] so that I can send it back to the embedded target. How do I grab the
    > data?


    Using BitConverter.GetBytes(float).

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Feb 22, 2005
    #7
  8. A MemoryStream is what it says a Stream in memory :)
    It provides handy functionality for methods requiring a Stream.
    In this case, the goal is the BinaryReader's ReadSingle method.
    Since BinaryReader requires a Stream, we create one ...

    byte[] floatarray = {123,123,123,123}; // some data defining a float

    MemoryStream ms = new MemoryStream();
    ms.Write(floatarray, 0, floatarray.Length);
    ms.Position = 0;

    BinaryReader br = new BinaryReader(ms, "ISO-8859-1");
    float f = br.ReadSingle();
    br.Close();

    Feed the stream some data
    Reset the position so we can read from the start
    Create a BinaryReader using the memorystream and an 8-bit encoding. The
    encoding is probably not necessary but it ensures the characters are
    considered one byte long when reading text.
    Now, simply assume it contains a series of bytes defining a float :)
    BinaryReader.Close closes both the Reader and the Stream.


    --
    Happy Coding!
    Morten Wennevik [C# MVP]
     
    Morten Wennevik, Feb 23, 2005
    #8
  9. Morten Wennevik <> wrote:
    > A MemoryStream is what it says a Stream in memory :)
    > It provides handy functionality for methods requiring a Stream.
    > In this case, the goal is the BinaryReader's ReadSingle method.


    No, the goal is to convert data from a byte array to a float. Although
    a BinaryReader is one way of doing that, it's not the only way - and
    the BitConverter way does it without creating any extra objects :)

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Feb 23, 2005
    #9
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael

    HOWTO? float to 4 Bytes, 4 Bytes to float

    Michael, Jul 29, 2003, in forum: Microsoft C# .NET
    Replies:
    3
    Views:
    1,411
    Michael
    Jul 29, 2003
  2. Cory Autry

    float array to byte array

    Cory Autry, Oct 12, 2003, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    397
    Austin Ehlers
    Oct 13, 2003
  3. Guest
    Replies:
    0
    Views:
    397
    Guest
    Mar 8, 2004
  4. Replies:
    1
    Views:
    203
    Jon Skeet [C# MVP]
    Dec 17, 2006
  5. cok119
    Replies:
    6
    Views:
    391
    gerry
    Jan 2, 2007
Loading...

Share This Page