PHP取进制余数函数代码

时间:2022-10-26 20:40:47

代码如下:


//取进制位上的数值 
function getRemainder($num, $bin, $pos, &$result = 0){ 
//author lianq.net 
//$num 数值,十进制 
//$bin 要转换的进制 
//$pos 位数 
$real_len = log($num, $bin);//对数,求原值长度 
$floor_len = floor($real_len);//舍去求整 
$base = pow($bin, $pos-1);//基数 
$divisor = pow($bin,$pos);//除数 
if($num >= $divisor){ 
$new_num = $num % pow($bin, $floor_len); 
getRemainder($new_num, $bin, $pos, $result); 
}else{ 
$result = floor($num / $base); 

return $result; 


//比如,数值16转换为9进制时,它的第一位上的数值是多少? 
$a = getRemainder(16,9, 1); 
echo $a;//输出7