How to read floats?

I just want to know how to read float values in php? My file contains binary value whose hex representation is ‘0xA418BA42’ which is actually float value ‘93.048126’. If I want to echo value ‘93.048126’ what should I do?

echo floatval($var); perhaps? Beats me why you have a float value stored as a hex string.

I have not stored float value as hex. When I open my file in hex editor, and select four bytes ‘A418BA42’ I get different data type values as

Signed Byte -92 Unsigned Byte 164 Signed Short 6308 Unsigned Short 6308 Signed Long 1119492260 Unsigned Long 1119492260 Float 93.048126 Binary 10100100000110001011101001000010

If I store that 32 bit value in variable $var and echo floatval($val), it echos 0.

Is hexdec() in combination with floatval() an option?

If I use hexdec() before floatval(), it echoes ‘11’.

$myfloatval = 93.048126; echo sprintf("%X", $myfloatval);

Gives me 5D as output, which makes sense: 5 * 16 + 13 = 93.
I’m starting to wonder how you managed to make A418BA42 represent your float value 93.048126?

‘A418BA42’ is a 32 bit value in binary file. If it is signed/unsigned long, then it’s value is ‘1119492260’ or if it is a float then it’s value is ‘93.048126’. I want a code that reads this value from binary file and echoes ‘93.048126’.

Can we see the code that turned 93.048126 into A418BA42?

I haven’t encoded that float into hex. If you are not getting what I’m telling, do one thing. Create simple text file in notepad. Write ‘1234’ in it and save that file. Now open that file in hex editor and select those four bytes. Data inspector of your hex editor will show you different data types values. e.g. If your selected data is byte, it will be 49(0x31) or if it is short then it has value 12849(0x3132), if long then 875770417(0x31323334), if float then 1.6688934e-007 for same ascii code (0x31323334). When you try to read this file normally in PHP it will echo value ‘1234’. I have code that reads this as ‘875770417’ and now I want read it as ‘1.6688934e-007’.


$data = “1234”;
$hex = dechex(ord(4)).dechex(ord(3)).dechex(ord(2)).dechex(ord(1));
$dec = hexdec($hex);
echo $dec;


This not the proper way of reading longs, but atleast it gives me what I want.

Sponsor our Newsletter | Privacy Policy | Terms of Service