当前位置:首页 > PHP教程 > php函数 > 列表

php实现36进制与10进制转换功能示例

发布:smiling 来源: PHP粉丝网  添加日期:2018-11-01 14:29:42 浏览: 评论:0 

本文实例讲述了php实现36进制与10进制转换功能。分享给大家供大家参考,具体如下:

  1. /** 
  2.  * @desc im:十进制数转换成三十六机制数 
  3.  * @param (int)$num 十进制数 
  4.  * return 返回:三十六进制数 
  5. */ 
  6. functionget_char($num) { 
  7.   $num=intval($num); 
  8.   if($num<= 0) 
  9.     returnfalse; 
  10.   $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'); 
  11.   $char='' 
  12.   do
  13.     $key= ($num- 1) % 36; 
  14.     $char=$charArr[$key] .$char
  15.     $num=floor(($num-$key) / 36); 
  16.   }while($num> 0); 
  17.   return$char
  18. /** 
  19.  * @desc im:三十六进制数转换成十机制数 
  20.  * @param (string)$char 三十六进制数 
  21.  * return 返回:十进制数 
  22.  */ 
  23. functionget_num($char){ 
  24.   $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"); 
  25.   $len=strlen($char); 
  26.   for($i=0;$i<$len;$i++){ 
  27.     $index=array_search($char[$i],$array); 
  28.     $sum+=($index+1)*pow(36,$len-$i-1); 
  29.   } 
  30.   return$sum

使用实例:

  1. echo"get_char:".get_char(514549)."<br>"
  2. echo"get_num:".get_num('A0ZZ')."<br>"

 

Tags: 36进制 10进制

分享到: