473,765 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CAtgGalvanoMach ine::Convert4By tesToFloat(BYTE byte1, BYTE byte2, BYTE
byte3, BYTE byte4)
{
DWORD l,ll,m,e;
float returnvalue=0,m antisse=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,un signed int(ldexp(1,i)) ))
{
mantisse+=float ((ldexp(1, i-23)));
}
}

returnvalue=flo at(ldexp(1+mant isse, 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 54816
>Is their now a ready function in the framework?
and for the other way float to 4 bytes?


Use the System.BitConve rter 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.ToDo uble(bytearray)

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


Try BitConverter.To Single()

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******** ******@TK2MSFTN GP12.phx.gbl...
but how do I Convert this Byte array back into float?

Their is a method for double :

BitConvert.ToDo uble(bytearray)

but this requiers a 8 Byte array. Theirs no BitConvert.ToFl oat()
Try BitConverter.To Single()

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
2036
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 page: http://members.aol.com/hikksnotathome/hideAndShowDivs/testPage.html IE6 and Opera7 display it as I want it.
1
2185
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 I have tested (IE5, IE5.5, IE6, NN6, NN7, Mozilla 1.7, FireFox0.8) except Opera, which refuses to respect my "float:right" rule (I think). Any solutions? Alternatively, is there a better way to achieve this same effect? I tried to do it...
3
2097
by: remi | last post by:
Can someone give me the smallest float(4 bytes) > 1.0f in oct and hex forms?
4
5373
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 integers, it is easy, I can get the 4 bytes by anding like: byte1 = int_val & 0x000000FF byte2 = int_val & 0x0000FF00 byte3 = int_val & 0x00FF0000
5
2396
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 it? the following code doesn't show up any error during compile time but crashes at runtime telling "unrecognized or unsupported array type": array<float>^ vals = gcnew array<float>(dimension); pin_ptr<float>ss=&vals; unmanaged_function(ss);
6
5464
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
3510
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 ( http://scripts.ringsworld.com/database-tools/xls2mysql/inc/Excel/reader.php.html ) Somewhere there happens a exponent to float conversion so that strings as 1E3 2E9 are converted to their floating point values. I'm trying to put this functionality off...
8
2270
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
1923
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 of the blue of box, only the orange 'AAA' box floated to the right, exactly as I expect. In IE, however, the blue box fills the page width 100%. It's as if it has lost its float. I've been searching for a few hours, and although there are a lot...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.