復制代碼 代碼如下:
//取進制位上的數(shù)值
function getRemainder($num, $bin, $pos, &$result = 0){
//author lianq.net
//$num 數(shù)值,十進制
//$bin 要轉(zhuǎn)換的進制
//$pos 位數(shù)
$real_len = log($num, $bin);//對數(shù),求原值長度
$floor_len = floor($real_len);//舍去求整
$base = pow($bin, $pos-1);//基數(shù)
$divisor = pow($bin,$pos);//除數(shù)
if($num >= $divisor){
$new_num = $num % pow($bin, $floor_len);
getRemainder($new_num, $bin, $pos, $result);
}else{
$result = floor($num / $base);
}
return $result;
}
//比如,數(shù)值16轉(zhuǎn)換為9進制時,它的第一位上的數(shù)值是多少?
$a = getRemainder(16,9, 1);
echo $a;//輸出7
新聞熱點
疑難解答