From byte[] to float and back again

  • Thread starter Thread starter Guest
  • Start date Start date
G

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#.
 
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.
 
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.
 
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. :)
 
Kenny ODell said:
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
 
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 said:
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 said:
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#.
 
Kenny ODell said:
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).
 
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.
 
Morten Wennevik said:
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 :)
 
Back
Top