Home > Backend Development > PHP Tutorial > PHP function to implement decimal and hexadecimal conversion

PHP function to implement decimal and hexadecimal conversion

WBOY
Release: 2016-07-25 08:57:50
Original
1114 people have browsed it
  1. /**
  2. * Base conversion: decimal, hexadecimal conversion
  3. * by bbs.it-home.org
  4. */
  5. $dic = array(
  6. 0 => '0', 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9',
  7. 10 => 'A', 11 => 'B', 12 => 'C', 13 => 'D', 14 => 'E', 15 => 'F', 16 => 'G', 17 => 'H', 18 => 'I',
  8. 19 => 'J', 20 => 'K', 21 => 'L', 22 => 'M', 23 => 'N', 24 => 'O', 25 => 'P', 26 => 'Q', 27 => 'R',
  9. 28 => 'S', 29 => 'T', 30 => 'U', 31 => 'V', 32 => 'W', 33 => 'X', 34 => 'Y', 35 => 'Z'
  10. );
  11. //十进制转换三十六进制
  12. function enid($int, $format = 8) {
  13. global $dic;
  14. $arr = array();
  15. $loop = true;
  16. while ($loop)
  17. {
  18. $arr[] = $dic[bcmod($int, 36)];
  19. $int = floor(bcdiv($int, 36));
  20. if ($int == 0) {
  21. $loop = false;
  22. }
  23. }
  24. array_pad($arr, $format, $dic[0]);
  25. return implode('', array_reverse($arr));
  26. }
  27. //三十六进制转换十进制
  28. function deid($id) {
  29. global $dic;
  30. // 键值交换
  31. $dedic = array_flip($dic);
  32. // 去零
  33. $id = ltrim($id, $dic[0]);
  34. // 反转
  35. $id = strrev($id);
  36. $v = 0;
  37. for($i = 0, $j = strlen($id); $i < $j; $i++)
  38. {
  39. $v = bcadd(bcmul($dedic[$id{$i}] , bcpow(36, $i)) , $v);
  40. }
  41. return $v;
  42. }
  43. // 遍历三位所有的三十六进制数
  44. $i = deid('ZZZ');
  45. $b = array();
  46. while ($i > 0) {
  47. $id_dym = str_pad(enid($i), 3, 0, STR_PAD_LEFT);
  48. echo strtolower($id_dym), '
    ';
  49. $i--;
  50. }
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template