473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 54693
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: HikksNotAtHome | last post by:
This page displays as I want it to (for the way its coded). http://members.aol.com/hikksnotathome/hideAndShowDivs/index.html When I try to move the Up/Down Arrow over to the right, as in this...
1
by: Kyle James Matthews | last post by:
Hello, I am curious if any of you have a solution to a problem I am having on this page: <http://www.digitalovertone.com/new/index2.php>. The navigation bar displays correctly on every browser...
3
by: remi | last post by:
Can someone give me the smallest float(4 bytes) > 1.0f in oct and hex forms?
4
by: Michael Yanowitz | last post by:
Hello: For some reason I can't figure out how to split a 4-byte (for instance) float number (such as 3.14159265359) into its 4-bytes so I can send it via a socket to another computer. For...
5
by: xxx | last post by:
Hi all, i'm new in visual c++ and i'm having troubles converting types. Let me explain: i have an unmanaged c++ function that wants an float* parameter but i have an array<float>^, how i can covert...
6
by: cok119 | last post by:
Hi, all How can I add '+' or '-' before float value using float.ToString(fotmat-string) ? What's the format string should used ? thanks
1
by: malick | last post by:
Hello it developers, I'm using PHPExcelReader which I downloaded from sourceforge (http://sourceforge.net/projects/phpexcelreader) To read excel xls files in php. In the file reader.php (...
8
by: Tom Impelluso | last post by:
Hi! I have used both of these "float *f" and "float* f" Could someone tell me if one is preferred and why? Yes, i know both
1
by: desheffer | last post by:
Hello all, I'm having an issue with (something like) the following: <div style="background:blue; float:right;"> <div style="background:orange; float:right;">AAA</div> </div> In FF, I see none...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.