|
|
|
start date: Thu, 23 Aug 2007 06:10:01 -0700,
posted on: microsoft.public.dotnet.languages.csharp
back
| Thread Index |
|
1
flyfisher1952
|
|
2
Ignacio Machin \( .NET/ C# MVP \) machin TA laceupsolutions.com
|
|
3
flyfisher1952
|
how to convert a bit pattern stored in 2 ushorts to a float
I am reading values from a device through a TCP/IP socket that represent an
ANSI float value. However, the values come across as 2 elements in a ushort
array. I need to map the 2 values bit patterns into a floating point number
to get the actual float values being returned.
In C++ I'd finagle it using pointers since a pointer is analogous to an
array. I could just create a ushort pointer, give it the address of a float
variable and assign the values to the pointer using array notation. The float
variable would then have the proper bit pattern.
I don't know how to do that type of thing in C#. Anyone out there know how?
Thanks,
Mike
Date:Thu, 23 Aug 2007 06:10:01 -0700
Author:
|
Re: how to convert a bit pattern stored in 2 ushorts to a float
Hi,
"flyfisher1952" wrote in message
news:9569D47D-57DD-40D7-A391-AA6AD2255A23@microsoft.com...
>I am reading values from a device through a TCP/IP socket that represent an
> ANSI float value. However, the values come across as 2 elements in a
> ushort
> array. I need to map the 2 values bit patterns into a floating point
> number
> to get the actual float values being returned.
>
> In C++ I'd finagle it using pointers since a pointer is analogous to an
> array. I could just create a ushort pointer, give it the address of a
> float
> variable and assign the values to the pointer using array notation. The
> float
> variable would then have the proper bit pattern.
>
> I don't know how to do that type of thing in C#. Anyone out there know
> how?
Take a look at BitConverter class
Date:Thu, 23 Aug 2007 09:20:34 -0400
Author:
|
Re: how to convert a bit pattern stored in 2 ushorts to a float
Here is the solution using BitConverter.
byte[] bytes = new byte[4];
byte[] highBytes = BitConverter.GetBytes(u1);
byte[] lowBytes = BitConverter.GetBytes(u2);
// The order is funky (little endian), but that's intel for you.
bytes[0] = lowBytes[0];
bytes[1] = lowBytes[1];
bytes[2] = highBytes[0];
bytes[3] = highBytes[1];
float fValue = BitConverter.ToSingle(bytes, 0);
return fValue;
--
Jack of All Trades, Master of None
"Ignacio Machin ( .NET/ C# MVP )" wrote:
> Hi,
>
> "flyfisher1952" wrote in message
> news:9569D47D-57DD-40D7-A391-AA6AD2255A23@microsoft.com...
> >I am reading values from a device through a TCP/IP socket that represent an
> > ANSI float value. However, the values come across as 2 elements in a
> > ushort
> > array. I need to map the 2 values bit patterns into a floating point
> > number
> > to get the actual float values being returned.
> >
> > In C++ I'd finagle it using pointers since a pointer is analogous to an
> > array. I could just create a ushort pointer, give it the address of a
> > float
> > variable and assign the values to the pointer using array notation. The
> > float
> > variable would then have the proper bit pattern.
> >
> > I don't know how to do that type of thing in C#. Anyone out there know
> > how?
>
> Take a look at BitConverter class
>
>
>
Date:Thu, 23 Aug 2007 08:28:03 -0700
Author:
|
|
|