高精度函数实现
/**
* 十进制数转换成62进制
*
* @param integer $num
* @return string
*/
function from10_to62($num) {
$to = 62;
$dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = '';
do {
$ret = $dict[bcmod($num, $to)] . $ret;
$num = bcdiv($num, $to);
} while ($num > 0);
return $ret;
}
/**
* 62进制数转换成十进制数
*
* @param string $num
* @return string
*/
function from62_to10($num) {
$from = 62;
$num = strval($num);
$dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$len = strlen($num);
$dec = 0;
for($i = 0; $i < $len; $i++) {
$pos = strpos($dict, $num[$i]);
$dec = bcadd(bcmul(bcpow($from, $len - $i - 1), $pos), $dec);
}
return $dec;
}
普通版
/**
* 10进制转为62进制
*
* @param integer $n 10进制数值
* @return string 62进制
*/
function dec62($n) {
$base = 62;
$index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = '';
for($t = floor(log10($n) / log10($base)); $t >= 0; $t --) {
$a = floor($n / pow($base, $t));
$ret .= substr($index, $a, 1);
$n -= $a * pow($base, $t);
}
return $ret;
}
/**
* 62进制转为10进制
*
* @param integer $n 62进制
* @return string 10进制
*/
function dec10($s) {
$base = 62;
$index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = 0;
$len = strlen($s) - 1;
for($t = 0; $t <= $len; $t ++) {
$ret += strpos($index, substr($s, $t, 1)) * pow($base, $len - $t);
}
return $ret;
}