首页 > Web开发 > 详细

php 36进制与10进制转换

时间:2016-03-04 19:03:17      阅读:175      评论:0      收藏:0      [点我收藏+]

php 36进制与10进制转换

[php] view plain copy
 
/** 
 * @desc  im:十进制数转换成三十六机制数 
 * @param (int)$num 十进制数 
 * return 返回:三十六进制数 
*/  
function get_char($num) {  
    $num = intval($num);  
    if ($num <= 0)  
        return false;  
    $charArr = array("0","1","2","3","4","5","6","7","8","9",A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);  
    $char = ‘‘;  
    do {  
        $key = ($num - 1) % 36;  
        $char= $charArr[$key] . $char;  
        $num = floor(($num - $key) / 36);  
    } while ($num > 0);  
    return $char;  
}  
  
/** 
 * @desc  im:三十六进制数转换成十机制数 
 * @param (string)$char 三十六进制数 
 * return 返回:十进制数 
 */  
function get_num($char){  
    $array=array("0","1","2","3","4","5","6","7","8","9","A", "B", "C", "D","E", "F", "G", "H", "I", "J", "K", "L","M", "N", "O","P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y","Z");  
    $len=strlen($char);  
    for($i=0;$i<$len;$i++){  
        $index=array_search($char[$i],$array);  
        $sum+=($index+1)*pow(36,$len-$i-1);  
    }  
    return $sum;  
} 

//使用实例:
echo "get_char:".get_char(514549)."<br>"; 

 
echo "get_num:".get_num(‘A0ZZ‘)."<br>";  
 

 

 

 

 

php 36进制与10进制转换

原文:http://www.cnblogs.com/archoncap/p/5243006.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!