eliteskills Posted July 18, 2008 Share Posted July 18, 2008 I have a binary file which only has 4 bytes "FFFFFFEE". I just want to put it in a variable equaling 4294967278. How I've failed so far: <? $foo= bin2hex(file_get_contents('testplz.bin')); // This correctly creates the hex number but on closer inspection it just turned it into a string. $foo=intval(bin2hex(file_get_contents('testplz.bin')),16); // This works up until the value exceeds 2147483647, there's a function called floatval but it doesn't accept the ,16 argument and doesn't accept binary either settype($foo, "float"); // Any variation of bin2hex, casting, etc don't seem to give me a correct answer echo (float)bin2hex(file_get_contents('testplz.bin')); ?> Please help ='/, I can't proceed with what I need to finish. Link to comment https://forums.phpfreaks.com/topic/115525-solved-turn-binary-number-0xffffffee-into-a-float/ Share on other sites More sharing options...
Barand Posted July 18, 2008 Share Posted July 18, 2008 <?php $n = floatval(hexdec('FFFFFFEE')); echo $n; // --> 4294967278 ?> Link to comment https://forums.phpfreaks.com/topic/115525-solved-turn-binary-number-0xffffffee-into-a-float/#findComment-593904 Share on other sites More sharing options...
eliteskills Posted July 18, 2008 Author Share Posted July 18, 2008 Thhhhhhhhhhank you!!! You have saved precious time. I just finished making this disgusting function which does the same thing. function bin2float($blah){ $arr=str_split(bin2hex($blah)); $out=0; $pow= strlen($blah); foreach($arr as $v){ $pow--; $out+=intval($v,16)*pow(16,$pow); } return $out; } echo bin2float(file_get_contents('testplz.bin')); Thanks for the much more elegant solution. Link to comment https://forums.phpfreaks.com/topic/115525-solved-turn-binary-number-0xffffffee-into-a-float/#findComment-593910 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.