PHP取进制余数函数代码_PHP
- WBOYOriginal
- 2016-06-01 12:13:39890browse
复制代码 代码如下:
//取进制位上的数值
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
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn