DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
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:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us