Comment on Tutorial - Convert a hex string into a 32-bit IEEE 754 float number in PHP By Julian L



Comment Added by : Mark

Comment Added at : 2015-02-02 14:42:56

Comment on Tutorial : Convert a hex string into a 32-bit IEEE 754 float number in PHP By Julian L
My code:

$hex=array('C4028000','457F9000','2D7F5','0002D7F5','47D9F95E','000040E4');

foreach($hex as $h){
echo $h.'='.hexTo32Float($h).'<br>';
}

function hexTo32Float($strHex) {
$v = hexdec($strHex);
$x = ($v & ((1 << 23) - 1)) + (1 << 23) * ($v >> 31 | 1);
$exp = ($v >> 23 & 0xFF) - 127;
return $x * pow(2, $exp - 23);
}

This outputs:

C4028000=522
457F9000=4089
2D7F5=6.0080426430695E-39
0002D7F5=6.0080426430695E-39
47D9F95E=111602.734375
000040E4=5.8891109391561E-39

The first output differs from yours as it is positive. The other values match yours. However, the last one (000040E4=5.8891109391561E-39) doesn't agree with http://babbage.cs.qc.cuny.edu/IEEE-754/index.xhtml, which tells me the decimal value of 000040E4 is 2.3278370089363861182184995837612086772827711378452680000428418331977209465577516311896033585071563720703125E-41

And http://babbage.cs.qc.cuny.edu/IEEE-754.old/32bit.html tells me it should be: 2.327837008936386e-41

Is this an error in the function or an error on the other site?

Cheers,
Mark.


View Tutorial



Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. i want very simple & easy notes on php which u
View Tutorial          By: deepa at 2011-07-12 09:04:30

2. when i was running this code... it is giving me th
View Tutorial          By: Nilesh Agrawal at 2012-06-08 15:08:25

3. also MEGA IMPORTANT:

make sure you

View Tutorial          By: niko at 2013-04-19 18:55:14

4. what ll be result for s1==s2 and s1.equals(s2) for
View Tutorial          By: vijay at 2011-08-21 15:09:41

5. You may add STAX parser example also to complete t
View Tutorial          By: Satyam Jaiswal at 2014-11-25 05:48:59

6. THANX FOR THE INFO....
View Tutorial          By: Abhinaykumar at 2010-07-06 11:34:53

7. thnx...nice article...useful..
View Tutorial          By: anish at 2012-11-30 05:41:47

8. string is immutable in java.String buffer is mutab
View Tutorial          By: krishnakumar at 2009-10-19 00:35:44

9. @Srini

Hi Srini, I can answer your

View Tutorial          By: Sudev Wilson at 2015-04-08 12:23:33

10. Real Life example of using Volatile
*******

View Tutorial          By: Mayank at 2011-01-25 15:44:21