449,412 Members | 993 Online Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

# int to float conversion

 P: n/a I am trying to calculate a 32bit float value from 4 int values. I sucessfully calcluated a 32bit long value: LONG l32BitData = pData; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData; 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 Replies

 P: n/a In article <35**************************@posting.google.com >, "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; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData; 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

 P: n/a "Mark A. Odell" wrote in message news:... In article <35**************************@posting.google.com >, "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; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData; 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

 P: n/a In article <35**************************@posting.google.com >, "Mike" 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

 P: n/a 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; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData; 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 #define BITS_MANT 23 #define BITS_EXP 8 #define MASK_EXP ((1UL<>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

 P: n/a Mike wrote: "Mark A. Odell" wrote in message news:...In article <35**************************@posting.google.com >, "Mike" wrote:I am trying to calculate a 32bit float value from 4 int values. Isucessfully calcluated a 32bit long value: LONG l32BitData = pData; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData; l32BitData <<= 8; l32BitData |= (byte)pData;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() functionWhy use a function?int myInt = 1234567;int myFloat = myInt;Done. There's your int to float conversion for free. What? Got an int packedinto bytes, first unpack the int into an int variable then assign it to afloat 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 mmmmmmmmmmmmmmmmmmmmmmmm 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

 P: n/a Try union { float aFloat; byte theBytes; } IntOrFloat; IntOrFloat.theBytes = pData; ect.... // be sure the bytes are in the right order or float aFloat; byte *pBytes; pBytes = (byte *)&aFloat; pBytes = pData; Nov 14 '05 #7

 P: n/a Neil Kurzman wrote in message news:<40***************@mail.asb.com>... Try float aFloat; byte *pBytes; pBytes = (byte *)&aFloat; pBytes = pData; Thanks - worked perfectly! Nov 14 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion. 