杜郎俊赏 - dujun.io

62进制转换

//十进制转为 62 进制
function encode62($number)
{
    $base = 62;
    $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $ret = '';
    for ($t = floor(log10($number) / log10($base)); $t >= 0; $t--) {
        $a = floor($number / pow($base, $t));
        $ret .= substr($index, $a, 1);
        $number -= $a * pow($base, $t);
    }
    return $ret;
}

//62 进制转为十进制
function decode62($number)
{
    $base = 62;
    $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $ret = 0;
    $len = strlen($number) - 1;
    for ($t = 0; $t <= $len; $t++) {
        $ret += strpos($index, substr($number, $t, 1)) * pow($base, $len - $t);
    }
    return $ret;
}

标签: 笔记
日期:2017-08-31