<html xmlns=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>PHP N进制转换</title> </head> <body> <?php /* * =========================================== * 项目: 进制转换 * 版本: 2.0 * 团队: 3Vshej * 作者: 网游世界 * 功能: 10进制转换n进制 n进制转换10进制 * =========================================== * Copyright (c) 2008-2009 * 团队主页: * 团队信箱: 3vshej+163.com[+=@] * 创建日期: 2008-11-3 * 修改日期: 2009-11-27 * 修改说明: 功能增强,优化算法,简化代码。 * 版权声明: 免费软件 * =========================================== */ class jinzhi_class{//进制转换类
var $mishao = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; var $jinwei = 1; //定义默认密钥及进位
function shezhi($ms,$jw){//设置密钥及进位
$this->mishao = $ms; $this->jinwei = $jw; }
function jz_xiao($shu,$jinzhi,$weishu){//小数到N进制转换
//将$shu转换为$jinzhi,文本长度为$weishu
$jieguo = "";//存储结果
$linshi = 1;//临时变量
$yushu = 1;//存储余数
$changdu = 1;//存储文本长度
$jiaoding = $shu;//存储原数值
//变量初始化
if ($shu == 0) $jieguo = substr($this->mishao,0,$this->jinwei);
while($shu != 0){ $linshi = intval($shu / $jinzhi); $yushu = $shu % $jinzhi; $jieguo = substr($this->mishao,$yushu*$this->jinwei,$this->jinwei).$jieguo; $shu = $linshi; }
$changdu = strlen($jieguo);//取结果文本长度
if ($changdu<$weishu)//如果不够位数则补短
$jieguo = $this->buduan($weishu-$changdu).$jieguo;
if ($this->fz_xiao($jinzhi,$jieguo)!=$jiaoding) return -1;
return $jieguo;//完成处理,返回结果。
}
function fz_xiao($jinzhi,$wenben){//小数进制反转换
$jieguo = ""; $changdu = intval(strlen($wenben)/$this->jinwei); $weizhi = 1; $zifu = ""; for ($i=1;$i<=$changdu;$i++){ if ($this->jinwei>1){ //多进位进制转换
$zifu = substr($wenben,$i*$this->jinwei-($this->jinwei),$this->jinwei); $jieguo += (intval(strpos($this->mishao,$zifu)/$this->jinwei))*pow($jinzhi,$changdu - $i); }else{ //单进位进制转换
$zifu = substr($wenben,$i*$this->jinwei-1,$this->jinwei); $jieguo += intval(strpos($this->mishao,$zifu))*pow($jinzhi,$changdu - $i); } } return $jieguo;//完成处理,返回结果。
}
function jz_da($shu,$jinzhi,$weishu){//大数到N进制转换
//将$shu转换为$jinzhi,文本长度为$weishu
bcscale(0); $jieguo = "";//存储结果
$linshi = 1;//临时变量
$yushu = 1;//存储余数
$changdu = 1;//存储文本长度
$jiaoding = $shu;//存储原数值
//变量初始化
if ($shu == "0") $jieguo = substr($this->mishao,0,$this->jinwei);
while($shu != "0"){ $linshi = bcdiv($shu,$jinzhi); $yushu = bcmod($shu,$jinzhi); $jieguo = substr($this->mishao,$yushu*$this->jinwei,$this->jinwei).$jieguo; $shu = $linshi; } $changdu = strlen($jieguo);//取结果文本长度
if ($changdu<$weishu)//如果不够位数则补短
$jieguo = $this->buduan($weishu-$changdu).$jieguo; //echo $jieguo,"<- ";
if ($this->fz_da($jinzhi,$jieguo)!=$jiaoding) return -1;
return $jieguo;//完成处理,返回结果。
}
function fz_da($jinzhi,$wenben){//大数进制反转换
bcscale(0);//设置没有小数位。
$jieguo = ""; $changdu = bcdiv(strlen($wenben),$this->jinwei); $weizhi = 1; $zifu = ""; for ($i=1;$i<=$changdu;$i++){ if ($this->jinwei>1){ //多进位进制转换
$zifu = substr($wenben,$i*$this->jinwei-($this->jinwei),$this->jinwei); $jieguo = bcadd(bcmul(bcdiv(strpos($this->mishao,$zifu),$this->jinwei),bcpow($jinzhi,$changdu - $i)),$jieguo); }else{ //单进位进制转换
$zifu = substr($wenben,$i*$this->jinwei-1,$this->jinwei); $jieguo = bcadd(bcmul(strpos($this->mishao,$zifu),bcpow($jinzhi,$changdu - $i)),$jieguo); } } return $jieguo;//完成处理,返回结果。
}
function buduan($geshu){//补短
$zifu = substr($this->mishao,0,$this->jinwei);//取默认为0的字符。
$jieguo = $zifu; for ($i=1;$i<=$geshu-1;$i++) $jieguo.=$zifu; return $jieguo;//完成处理,返回结果。
} } $mtime1 = explode(" ", microtime()); $startTime = $mtime1[0] + $mtime1[1];
$mishao = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";//密钥
$jinwei = 1;//进位
$shu = "2147483647";//数据
$jinzhi = 16;//进制
$weishu = 5;//位数,0为自动
$jz = new jinzhi_class; //$jz->shezhi($mishao,$jinwei);
$jieguo = $jz->jz_xiao($shu,$jinzhi,$weishu); echo "
" ; echo "使用说明: 1、转换的数值不能小于0。 2、普通转换最大值不能大于 2147483647。 3、进行进位运算时,最大进制值为,密钥长度除以进位。 当进行1进位的运算时,最大进制值为密钥长度。 4、默认密钥为:0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; echo "
"; echo "设置 密钥为:$mishao "; echo "设置 进位为:$jinwei "; echo "设置 长度为:$weishu "; echo " $shu 转换为 $jinzhi 进制 = $jieguo "; echo "进位:$jinwei ,实际长度:".strlen($jieguo)." ";
$jinwei = 1;//进位
$shu = "99999999999999999999";//数据
$jinzhi = 32;//进制
$weishu = 0;//位数,0为自动
$jz->shezhi($mishao,$jinwei); $jieguo = $jz->jz_da($shu,$jinzhi,$weishu); echo " "; echo "设置 密钥为:$mishao "; echo "设置 进位为:$jinwei "; echo "设置 长度为:$weishu "; echo " $shu 转换为 $jinzhi 进制 = $jieguo "; echo "进位:$jinwei ,实际长度:".strlen($jieguo).""; echo " "; $mtime1 = explode(" ", microtime()); $endTime = $mtime1[0] + $mtime1[1]; printf ("页面执行时间:%.6fs.
|