PHP takes the base remainder function code

  • 2020-05-10 17:54:25
  • OfStack

 
// Take the number in the base bit  
function getRemainder($num, $bin, $pos, &$result = 0){ 
//author lianq.net 
//$num  The numerical ,10 Into the system  
//$bin  The base to be converted  
//$pos  digits  
$real_len = log($num, $bin);// logarithmic , Find the original length  
$floor_len = floor($real_len);// Give up for the whole  
$base = pow($bin, $pos-1);// base  
$divisor = pow($bin,$pos);// divisor  
if($num >= $divisor){ 
$new_num = $num % pow($bin, $floor_len); 
getRemainder($new_num, $bin, $pos, $result); 
}else{ 
$result = floor($num / $base); 
} 
return $result; 
} 

// For example, the number 16 convert 9 In base, it's number one 1 What is the value in bits?  
$a = getRemainder(16,9, 1); 
echo $a;// The output 7 

Related articles: