php實現36進位與10進位轉換功能的方法

來源:互聯網
上載者:User
這篇文章主要介紹了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>";

以上就是本文的全部內容,希望對大家的學習有所協助。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.