Jump to content

[SOLVED] Turn binary number 0xFFFFFFEE into a float?


eliteskills

Recommended Posts

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.