這篇文章主要介紹了php實現36進制與10進制轉換功能,結合實例形式分析了php數值運算與字符串操作的相關技巧,需要的朋友可以參考下
本文實例講述了php實作36進位與10進位轉換功能。分享給大家供大家參考,具體如下:
/** * @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實作xml
為HTML
#PHP進位###轉換###實例詳解###########################
以上是php實作36進位與10進位轉換功能的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!