473,766 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

int to float conversion

I am trying to calculate a 32bit float value from 4 int values.

I sucessfully calcluated a 32bit long value:

LONG l32BitData = pData[3];

l32BitData <<= 8;
l32BitData |= (byte)pData[2];
l32BitData <<= 8;
l32BitData |= (byte)pData[1];
l32BitData <<= 8;
l32BitData |= (byte)pData[0];

But when I use type casting I (unsuprisingly) get the wrong value of
float.

Does anyone know a good way of calculating this? An IntToFloat()
function would be perfect or a description of the steps required to
calculate this. I have read how to calculate an intiger value from a
float but the required maths to do the reverse is beyond me.

Any help greatly appreciated.

Mike
Nov 14 '05 #1
7 26169
In article <35************ **************@ posting.google. com>, "Mike"
<mi***********@ hotmail.com> wrote:
I am trying to calculate a 32bit float value from 4 int values. I
sucessfully calcluated a 32bit long value:
LONG l32BitData = pData[3];

l32BitData <<= 8;
l32BitData |= (byte)pData[2];
l32BitData <<= 8;
l32BitData |= (byte)pData[1];
l32BitData <<= 8;
l32BitData |= (byte)pData[0];
But when I use type casting I (unsuprisingly) get the wrong value of float.
Does anyone know a good way of calculating this? An IntToFloat() function


Why use a function?

int myInt = 1234567;
int myFloat = myInt;

Done. There's your int to float conversion for free. What? Got an int packed
into bytes, first unpack the int into an int variable then assign it to a
float variable.
Nov 14 '05 #2
"Mark A. Odell" <od*******@hotm ail.com> wrote in message news:<c7******* ****@ID-74514.news.uni-berlin.de>...
In article <35************ **************@ posting.google. com>, "Mike"
<mi***********@ hotmail.com> wrote:
I am trying to calculate a 32bit float value from 4 int values. I
sucessfully calcluated a 32bit long value:
LONG l32BitData = pData[3];

l32BitData <<= 8;
l32BitData |= (byte)pData[2];
l32BitData <<= 8;
l32BitData |= (byte)pData[1];
l32BitData <<= 8;
l32BitData |= (byte)pData[0];
But when I use type casting I (unsuprisingly) get the wrong value of float.
Does anyone know a good way of calculating this? An IntToFloat() function


Why use a function?

int myInt = 1234567;
int myFloat = myInt;

Done. There's your int to float conversion for free. What? Got an int packed
into bytes, first unpack the int into an int variable then assign it to a
float variable.


Thanks for replying - I did say I tried type casting already.

When I use 0xAA (-86) as my value for each of the 4 bytes I get a
float value of -1.4317e9, but when I use Hex Workshop it shows the
correct value as -3.0316488e-013.

I know that to calculate the int from a float the following is used:

nValue = (-1)^s * 2^(exponent-127) * mantissa

the 32 bits in the long are represented as follows:

s | exponent | mantisa
1bit | 8bits | 23 bits

I just dont know how to do the reverse ...
Nov 14 '05 #3
In article <35************ **************@ posting.google. com>, "Mike"
<mi***********@ hotmail.com> wrote:
Why use a function?
int myInt = 1234567;
int myFloat = myInt;
Done. There's your int to float conversion for free. What? Got an int
packed into bytes, first unpack the int into an int variable then assign it
to a float variable.

Thanks for replying - I did say I tried type casting already. When I use
0xAA (-86) as my value for each of the 4 bytes I get a float value of
-1.4317e9, but when I use Hex Workshop it shows the correct value as
-3.0316488e-013.
I know that to calculate the int from a float the following is used: nValue
= (-1)^s * 2^(exponent-127) * mantissa the 32 bits in the long are
represented as follows:
s | exponent | mantisa
1bit | 8bits | 23 bits
I just dont know how to do the reverse ...


I clearly don't understand the problem. First, floating point is not portable.
So, don't depend on a floating point bit representation to mean the same thing
on another machine. For portablility you could sprintf() the float into a string
and then convert the string back into a float later but I wouldn't try packing
the float bytes into an int for transport over any sort of communication
link.
Nov 14 '05 #4
On 4 May 2004, Mike wrote:
I am trying to calculate a 32bit float value from 4 int values.
I sucessfully calcluated a 32bit long value:

LONG l32BitData = pData[3];
l32BitData <<= 8;
l32BitData |= (byte)pData[2];
l32BitData <<= 8;
l32BitData |= (byte)pData[1];
l32BitData <<= 8;
l32BitData |= (byte)pData[0];

But when I use type casting I (unsuprisingly) get the wrong value of
float.

Does anyone know a good way of calculating this? An IntToFloat()
function would be perfect or a description of the steps required to
calculate this. I have read how to calculate an intiger value from a
float but the required maths to do the reverse is beyond me.


You mean like the intBitsToFloat method in Java?

#include <math.h>

#define BITS_MANT 23
#define BITS_EXP 8

#define MASK_EXP ((1UL<<BITS_EXP )-1)
#define MASK_MANT ((1UL<<BITS_MAN T)-1)
#define MASK_S (1UL<<BITS_EXP+ BITS_MANT)
#define MINEXP 0
#define MAXEXP ((1UL<<BITS_EXP )-1)
#define HIDDEN_BIT (1UL<<BITS_MANT )
#define MAGIC_EXP (MAXEXP/2+BITS_MANT)

float ulongBitsToFloa t(unsigned long v) {
unsigned long ex = v>>BITS_MANT & MASK_EXP;
unsigned long man = v & MASK_MANT;
float mulby = pow(2, (long)ex-(long)MAGIC_EXP );

if (ex==MAXEXP) return v&MASK_S ? -FLT_MAX : FLT_MAX;
if (ex==MINEXP) mulby *= 2; else man |= HIDDEN_BIT;
return v&MASK_S ? man*-mulby : man*mulby;
}

I haven't written it the other way around yet. It's a bit harder,
but probably should go like:

tear apart the sign
init exponent to MAGIC_EXP
shift the value till it's in the "magic" range or exponent gets clamped.
deal with queer values
combine (unsigned long)f, exponent and sign.
Alternatively you could of course just peek at the object
representation of the float value if you don't need to move
the data around different machines (as the representations
may differ). This is done by casting a pointer to the data
into char * and accessing (sizeof data) bytes thru it.

Nov 14 '05 #5
Mike wrote:
"Mark A. Odell" <od*******@hotm ail.com> wrote in message news:<c7******* ****@ID-74514.news.uni-berlin.de>...
In article <35************ **************@ posting.google. com>, "Mike"
<mi********** *@hotmail.com> wrote:

I am trying to calculate a 32bit float value from 4 int values. I
sucessfull y calcluated a 32bit long value:
LONG l32BitData = pData[3];

l32BitData <<= 8;
l32BitData |= (byte)pData[2];
l32BitData <<= 8;
l32BitData |= (byte)pData[1];
l32BitData <<= 8;
l32BitData |= (byte)pData[0];
But when I use type casting I (unsuprisingly) get the wrong value of float.
Does anyone know a good way of calculating this? An IntToFloat() function


Why use a function?

int myInt = 1234567;
int myFloat = myInt;

Done. There's your int to float conversion for free. What? Got an int packed
into bytes, first unpack the int into an int variable then assign it to a
float variable.

Thanks for replying - I did say I tried type casting already.

When I use 0xAA (-86) as my value for each of the 4 bytes I get a
float value of -1.4317e9, but when I use Hex Workshop it shows the
correct value as -3.0316488e-013.

I know that to calculate the int from a float the following is used:

nValue = (-1)^s * 2^(exponent-127) * mantissa

the 32 bits in the long are represented as follows:

s | exponent | mantisa
1bit | 8bits | 23 bits

I just dont know how to do the reverse ...


You might have it slightly wrong. Consider..

10101010 10101010 10101010 10101010
Exp = 85 (-41)
11010111
Man = .10101010 10101010 10101010
-3.03164883e-13

The mantissa is actually 24 bits. Because (except in the case of
0.0) the high order bit of the normalized mantissa is always 1, its
position in the representation can be used by the low order of the
exponent. So we have 33 bits something like..

s
eeeeeeee
mmmmmmmmmmmmmmm mmmmmmmmm

My view of it has the mantissa a fraction (the . before the first m)
and the offset of the exponent to be 126 (not 127).

Have fun.
--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #6


Try

union
{
float aFloat;
byte theBytes[4];
} IntOrFloat;

IntOrFloat.theB ytes[0] = pData[0];
ect.... // be sure the bytes are in the right order

or
float aFloat;
byte *pBytes;

pBytes = (byte *)&aFloat;
pBytes[0] = pData[0];



Nov 14 '05 #7
Neil Kurzman <ns*@mail.asb.c om> wrote in message news:<40******* ********@mail.a sb.com>...
Try
float aFloat;
byte *pBytes;

pBytes = (byte *)&aFloat;
pBytes[0] = pData[0];


Thanks - worked perfectly!
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
22804
by: pavithra.eswaran | last post by:
Hi, I would like to convert a single precision hexadecimal number to floating point. The following program seems to work fine.. But I do not want to use scanf. I already have a 32 bit hexadecimal number and would like to convert it into float. Can anyone tell me how to do it? int main() { float theFloat;
1
4814
by: yoes | last post by:
Dear all, I am very new in MySQL, I am working on image database project for my research and I have problem how to convert blob field into float in MySQL so that I can calculate the blob field with the MATH operation. Does any one how to do it ???? Thank you very much indeed , for your help.
5
2397
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);
13
8839
by: volosov | last post by:
I would really appreciate if someone could give me a hint how these floats are encoded: 0B7D2F70 41.8713 0B7D232F 41.8687 0B7D220F 41.8684 Thanks in advance Vadim
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...
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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
10009
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...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
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
6651
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();...
1
3929
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.