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

本文实例讲述了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. /**
  20. * @desc im:三十六进制数转换成十机制数
  21. * @param (string)$char 三十六进制数
  22. * return 返回:十进制数
  23. */
  24. functionget_num($char){
  25. $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");
  26. $len=strlen($char);
  27. for($i=0;$i<$len;$i++){
  28. $index=array_search($char[$i],$array);
  29. $sum+=($index+1)*pow(36,$len-$i-1);
  30. }
  31. return$sum;
  32. }

使用实例:

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