HOWTO? float to 4 Bytes, 4 Bytes to float

M

Michael

Hallo NG,

a few jears ago i made myself in c++ a funktion to convert 4 Byte to one
float variable:

//###########################################################
// Make from 4 Bytes one float variable wich is 4 bytes long
//###########################################################
float CAtgGalvanoMachine::Convert4BytesToFloat(BYTE byte1, BYTE byte2, BYTE
byte3, BYTE byte4)
{
DWORD l,ll,m,e;
float returnvalue=0,mantisse=0;

l = byte1;
l = l << 24;
ll = byte2;
l = l | (ll << 16);
ll = byte3;
l = l | (ll << 8);
ll = byte4;
l = l | ll;

e=l&0x7f800000;
e>>=23;
m=l&0x7fffff;

for(int i=0; i<=23;i++)
{
if(TestBit(m,unsigned int(ldexp(1,i))))
{
mantisse+=float((ldexp(1, i-23)));
}
}

returnvalue=float(ldexp(1+mantisse, e-127));

return returnvalue;
}


Is their now a ready function in the framework?
and for the other way float to 4 bytes?

Regards

Michael
 
O

Oscar Papel

but how do I Convert this Byte array back into float?
Their is a method for double :

BitConvert.ToDouble(bytearray)

but this requiers a 8 Byte array. Theirs no BitConvert.ToFloat()

Try BitConverter.ToSingle()

This is a single precision IEEE-754 floating point number (a float in other
words)

Oscar.
 
M

Michael

Thanks,

thats it

Regards

Michael
Oscar Papel said:
Try BitConverter.ToSingle()

This is a single precision IEEE-754 floating point number (a float in other
words)

Oscar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top