首頁  >  文章  >  後端開發  >  php實作36進位與10進位轉換功能範例

php實作36進位與10進位轉換功能範例

高洛峰
高洛峰原創
2017-01-12 14:57:291316瀏覽

本文實例講述了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",&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;);
  $char = &#39;&#39;;
  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(&#39;A0ZZ&#39;)."<br>";

希望本文所述對大家PHP程式設計有所幫助。

更多php實現36進位與10進位轉換功能範例相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn