PHP取进制余数函数代码

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

//取进制位上的数值
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

相关文章

PHP加速 eAccelerator配置和使用指南

前一段时间完成了服务器从FreeBSD4.10到6.1的升级,同时把PHP也升级到了最新的PHP5.1.4,Apache也升级到了最新的Apache2.2,为了更好的提高系统的性能考虑对...

简单的过滤字符串中的HTML标记

function deleteHtml( $scr ) { $l = strlen( $scr );  for( $i=0; $i<$l; $i++ )&nbs...

详解php 使用Callable Closure强制指定回调类型

详解php 使用Callable Closure强制指定回调类型 如果一个方法需要接受一个回调方法作为参数,我们可以这样写 <?php function testC...

PHP扩展Memcache分布式部署方案

基础环境 其实基于PHP扩展的Memcache客户端实际上早已经实现,而且非常稳定。先解释一些名词,Memcache是danga.com的一个开源项目,可以类比于MySQL这样的服务,...

php实现基于openssl的加密解密方法

本文实例讲述了php实现基于openssl的加密解密方法。分享给大家供大家参考,具体如下: 通过openssl加密解密方法 1. openssl加密方法: function encr...