国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php實現(xiàn)的任意進制互轉(zhuǎn)類分享

2024-05-04 23:37:23
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php實現(xiàn)的任意進制互轉(zhuǎn)類分享,本文直接給出了實現(xiàn)代碼,需要的朋友可以參考下

之前不知道php自帶有base_convert可以實現(xiàn)任意進制之間的轉(zhuǎn)換,自己寫了一個。。。。

 

 
  1. <?php  
  2. /** 
  3. * 進制轉(zhuǎn)換類  
  4. * @author sgf@funcity 
  5. * @version 2011-02-15 
  6. */ 
  7. Class Hex{ 
  8.  
  9. private static $element = array
  10. '0','1','2','3','4','5','6','7','8','9'
  11. 'A','B','C','D','E','F','G','H','I','J'
  12. 'K','L','M','N','O','P','Q','R','S','T'
  13. 'U','V','W','X','Y','Z' 
  14. ); 
  15.  
  16. private static $hex_min = 2; 
  17. private static $hex_max = 36; 
  18.  
  19.  
  20. /** 
  21. * 進制轉(zhuǎn)換 
  22. */ 
  23. public function conv($int,$out_hex,$in_hex=10,$use_system=true){ 
  24.  
  25. if($use_system && function_exists('base_convert')){ 
  26. return strtoupper(base_convert($int,$in_hex,$out_hex)); 
  27.  
  28. if($out_hex == $in_hex){ 
  29. return $int
  30. if($out_hex > self::$hex_max || $out_hex < self::$hex_min){ 
  31. return false; 
  32. if($in_hex > self::$hex_max || $in_hex < self::$hex_min){ 
  33. return false; 
  34. $hex_10 = $this->_conv2hex10($int,$in_hex); 
  35. return strtoupper($this->_conv_hex($hex_10,$out_hex)); 
  36.  
  37. /** 
  38. * 將任意進制數(shù)字轉(zhuǎn)為10進制數(shù)字 
  39. */ 
  40. private function _conv2hex10($int,$in_hex){ 
  41. $int = strtoupper(trim($int)); 
  42. if($in_hex==10){ 
  43. return $int
  44. }elseif$in_hex== 2 && function_exists('bindec')){ 
  45. return bindec($int); 
  46. elseif($in_hex== 16 && function_exists('hexdec')){ 
  47. return hexdec($int); 
  48. elseif($in_hex== 8 && function_exists('octdec')){ 
  49. return octdec($int); 
  50. $array = array(); 
  51. $result = 0; 
  52. for($i=0;$i<strlen($int);$i++){ 
  53. array_unshift$arraysubstr($int,$i,1)); //插入到數(shù)組頭部(既倒序) 
  54. foreach($array as $k => $v){ 
  55.  
  56. $hex10_value = array_search($v,self::$element); 
  57. if($hex10_value==-1){ 
  58. return false; 
  59. $result += intval( pow($in_hex,$k) * $hex10_value ); 
  60.  
  61. return $result
  62.  
  63. /** 
  64. * 把10進制數(shù)換成任意進制數(shù) 
  65. */ 
  66. private function _conv_hex($hex_10,$out_hex){ 
  67.  
  68. $hex_10 = intval($hex_10); 
  69.  
  70. if($out_hex==10){ 
  71. return $hex_10
  72. }else if$out_hex==2 && function_exists('decbin')){ 
  73. return decbin($hex_10); 
  74. elseif ( $out_hex ==16 && function_exists('dechex')){ 
  75. return dechex($hex_10); 
  76. elseif ( $out_hex ==8 && function_exists('decoct')){ 
  77. return decoct($hex_10); 
  78.  
  79. $array = array(); 
  80. $result = "";  
  81.  
  82. //利用10進制數(shù)除任意進制數(shù) 倒取余數(shù)法轉(zhuǎn)換。 
  83. do { 
  84. array_unshift$array$hex_10 % $out_hex); //余數(shù)插入到數(shù)組數(shù)組第1個位置。 
  85. $hex_10 = $hex_10 / $out_hex ; //除法 
  86. while ($hex_10>1); 
  87.  
  88. foreach($array as $k){ 
  89. $result .= self::$element[$k];  
  90. return $result;  
  91.  
  92.  
  93. ?> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 西宁市| 溆浦县| 绥宁县| 宣恩县| 昭苏县| 维西| 施甸县| 华蓥市| 吐鲁番市| 江永县| 陵水| 寿光市| 关岭| 车致| 林芝县| 当涂县| 日土县| 玉田县| 阿克苏市| 财经| 赤水市| 兴义市| 墨脱县| 阳高县| 马山县| 库伦旗| 烟台市| 伽师县| 三明市| 墨竹工卡县| 胶州市| 凤城市| 东丰县| 乾安县| 乌拉特中旗| 潢川县| 博客| 阳城县| 温州市| 西华县| 万盛区|