PC Review


Reply
Thread Tools Rate Thread

From byte[] to float and back again

 
 
=?Utf-8?B?S2VubnkgT0RlbGw=?=
Guest
Posts: n/a
 
      22nd Feb 2005
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#.
 
Reply With Quote
 
 
 
 
Morten Wennevik
Guest
Posts: n/a
 
      22nd Feb 2005
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]
 
Reply With Quote
 
 
 
 
Dennis Myrén
Guest
Posts: n/a
 
      22nd Feb 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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#.



 
Reply With Quote
 
=?Utf-8?B?S2VubnkgT0RlbGw=?=
Guest
Posts: n/a
 
      22nd Feb 2005
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]
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Feb 2005
Kenny ODell <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?S2VubnkgT0RlbGw=?=
Guest
Posts: n/a
 
      22nd Feb 2005
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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#.

>
>
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Feb 2005
Kenny ODell <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      23rd Feb 2005
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]
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Feb 2005
Morten Wennevik <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I add '+' or '-' before float value using float.ToString() ? cok119 Microsoft C# .NET 6 2nd Jan 2007 06:22 PM
array<float>^ to float* conversion xxx Microsoft VC .NET 5 4th Oct 2006 06:36 PM
managed float != unmanaged float Amir Kolsky via .NET 247 Microsoft VC .NET 1 6th Jul 2004 04:40 PM
TypeLoadException: C# interface member float a2[,], implemented as CPP float get_a2 __gc [,] =?Utf-8?B?SGFucyBCZXJlbmRl?= Microsoft C# .NET 0 8th Mar 2004 05:11 PM
HOWTO? float to 4 Bytes, 4 Bytes to float Michael Microsoft C# .NET 3 29th Jul 2003 03:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:40 AM.