Home > php教程 > php手册 > body text

10进制整数转62进制的函数

WBOY
Release: 2016-06-21 08:48:53
Original
1689 people have browsed it

 

/** 
 * 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;  
}  
Copy after login

/** 
 * 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;  
Copy after login

 



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template