note 25404 added to language.operators.bitwise
| From: | zaharyk at mail dot bg | Date: | Sun, 22 Sep 2002 17:38:30 +0000 |
| Subject: | note 25404 added to language.operators.bitwise | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-37114@lists.php.net to get a copy of this message | ||
Here is an example for "raw" reading of Float variables.It could be very useful ,when
loading data from files or network sockets.
$bytestring = "\xC2\xE2\xA0\x00"; //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;}
--
http://www.php.net/manual/en/language.operators.bitwise.php
http://master.php.net/manage/user-notes.php?action=edit+25404
http://master.php.net/manage/user-notes.php?action=delete+25404
http://master.php.net/manage/user-notes.php?action=reject+25404