Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert 4 bytes to IEEE-754 float

Status
Not open for further replies.

slapierre

Programmer
Feb 3, 2004
16
0
0
CA
Greeting's

I need to convert 4 bytes in a float, I searched the forums but the answers were all about VB and I can't find an equivalent to the BitConverter method, here's a piece of code from a thread by theoxyde :

byte[] input = new byte[4]; // 32-bit byte array
// put your bytes into the array in correct order
float f = BitConverter.ToSingle( input, 0 );

I can do the bit sorting (it seems ok - I compare the bytes with a Matlab array that was writted by our client) the problem is that I'm unable to tell the compiler to interpret the bits in the field as a float32. Is there (of course there is) an operator or a method that can take the raw data and interpret it as a float?

Simon Lapierre
slapierre@sonomax.com
 
If I undestood your problem correctly, there should be no problem if data layout in memory is correct. For example :

BYTE bFloatData[4];
float fValue = *((float *)bFloatData);
 
thx a lot!

it's working great, all i had to do is to invert my bytes(array[3] = b0 ... array[0] = b3) and the digits matched!

Simon Lapierre
slapierre@sonomax.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top