PHP手册中只有最多到32进制转换的函数,64进制的没有,这里我提供两个我写的10to64和64to10转换的函数。这两个函数没考虑小数的情况,仅仅为整数的转换。此文由ArthurXF倾情奉献。
引用
/**
* 将64进制的数值转换为10进制的数值
* @author  肖飞 (ArthurXF)
* @param  string  $bit64    指定的64进制字符串
* @return  double
*/
function make_bit10($bit64) {
 $strLetter = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/';    
 $intCount = strlen($bit64);    
 for($i=0;$i<$intCount;$i++) {
   $pos = strpos($strLetter,$bit64[$i]);
   $bit10 += $pos * pow(64,$intCount-1-$i);
 }
 return $bit10;
}

/**
* 将10进制的数值转换为64进制的数值
* @author  肖飞 (ArthurXF)
* @param  double    $bit10    指定10进制数字
* @return  string
*/
function make_bit64($bit10) {
 $strLetter = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/';    //很多大于int取值范围的大数是以字符串形式传入的,这里强制转成double,否则求余会有误差
 settype($bit10,"double");
 $intCount = ceil($bit10/64);    
 for($i=0;$bit10>0;$i++) {
   $key = $bit10 % 64;
   if($key<0) $key = 64 + $key;
   $bit64 .= $strLetter[$key];
   $bit10 = floor($bit10 / 64);
 }
 return strrev($bit64);
}
PHP | 评论(0) | 引用(0) | 阅读(7908)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]