note 25403 deleted from language.operators.bitwise by sniper
| From: | sniper@php.net | Date: | Mon, 13 Oct 2003 00:12:46 +0000 |
| Subject: | note 25403 deleted from language.operators.bitwise by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58408@lists.php.net to get a copy of this message | ||
Note Submitter: zaharyk@mail.bg
----
Here is an example for "raw" reading of Float variables.It could be very useful ,when
loading data from files or network sockets.
$bytestring = "xC2xE2xA0x00"; //this is the binary representation of the number
"-113.3125"
function getfloat ($bytes) {
$bits = 0;
$len = strlen($bytes);
if($len == 4){
$explen = 8 ;
$mantisa = 23;
for($n=0;$n<$len;$n++){
$bits |= (ord($bytes[$n]) << ($len-$n-1)*8);
}
}
else if($len == 1){
$explen = 3;
$mantisa = 4;
$bits = ord($bytes[0]);
}
else {return false;}
if ($bits & (1 << ($mantisa + $explen))) {$out = "-";}
$stepen = ((((pow(2,$explen)-1) << $mantisa) & $bits) >> $mantisa) -
pow(2,$explen-1) + 1 ;
$ednocqloi = (pow(2,$mantisa)-1) & $bits ;
$out .= (1 + $ednocqloi/pow(2,$mantisa)) * pow(2,$stepen) ;
return $out ;
}
if(!$floatvar = getfloat($bytestring)) {echo "invalid byte/word";}
else {echo $floatvar;}