469,271 Members | 1,718 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

HOWTO? float to 4 Bytes, 4 Bytes to float

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
Nov 15 '05 #1
3 53540
>Is their now a ready function in the framework?
and for the other way float to 4 bytes?


Use the System.BitConverter class.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
> 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.

Nov 15 '05 #3
Thanks,

thats it

Regards

Michael
"Oscar Papel" <oe*****@rogers.com> schrieb im Newsbeitrag
news:O6**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by HikksNotAtHome | last post: by
1 post views Thread by Kyle James Matthews | last post: by
3 posts views Thread by remi | last post: by
4 posts views Thread by Michael Yanowitz | last post: by
5 posts views Thread by xxx | last post: by
8 posts views Thread by Tom Impelluso | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.