當前位置:首頁 > 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進制

分享到:

福利彩票25选5开奖结果