By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

From byte[] to float and back again

P: n/a
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#.
Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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]
Nov 16 '05 #2

P: n/a
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" <Ke********@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
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#.

Nov 16 '05 #3

P: n/a
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]

Nov 16 '05 #4

P: n/a
Kenny ODell <Ke********@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
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" <Ke********@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
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#.


Nov 16 '05 #6

P: n/a
Kenny ODell <Ke********@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

P: n/a
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]
Nov 16 '05 #8

P: n/a
Morten Wennevik <Mo************@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.