当前位置:Gxlcms > PHP教程 > php实现将任意进制数转换成10进制的方法,php进制_PHP教程

php实现将任意进制数转换成10进制的方法,php进制_PHP教程

时间:2021-07-01 10:21:17 帮助过:62人阅读

php实现将任意进制数转换成10进制的方法,php进制


本文实例讲述了php实现将任意进制数转换成10进制的方法。分享给大家供大家参考。具体如下:

php将任意进制的数转换成10进制,例如8进制转换成10进制,16进制转换成10进制

  1. <?php
  2. # Show the steps involved in converting a number
  3. # from any base (like octal or hex) to base 10
  4. # See below for examples, instructions and copyright
  5. function show_convert_to_base_10 ($number, $base)
  6. {
  7. // If the number contains a decimal component
  8. if (strstr ($number, '.'))
  9. {
  10. // Get the integer and decimal components
  11. list ($integer, $decimal) = explode ('.', $number);
  12. }
  13. else
  14. {
  15. // The number is an integer
  16. $integer = $number;
  17. }
  18. print "<b>Convert the base $base number $number to a
  19. base 10 number:</b><blockquote>";
  20. print "Convert the integer component ($integer) of the
  21. number:<blockquote>";
  22. // Compute the value of the integer component
  23. // Loop through the integer digit by digit
  24. // Reverse the number for easier handling
  25. $integer = strrev ($integer);
  26. $length = strlen ($integer);
  27. for ($pos = 0; $pos < $length; ++$pos)
  28. {
  29. /*
  30. PHP lets you treat strings and numbers like arrays
  31. Specify an offset and get the character at that
  32. position
  33. */
  34. $digit = $integer[$pos];
  35. // Handle character values for digits
  36. // (for bases greater than 10)
  37. if (eregi ('[a-z]', $digit))
  38. {
  39. $digit_value =
  40. (ord (strtolower ($digit))
  41. - ord ('a')) + 10;
  42. $digit = "$digit ($digit_value)";
  43. }
  44. else
  45. {
  46. $digit_value = $digit;
  47. }
  48. // Multiply the current digit by the radix
  49. // raised to the power of the current position
  50. $result = $digit_value * pow ($base, $pos);
  51. print "Multiply the value of the digit at position
  52. $pos by the value of the radix ($base) raised
  53. to the power of the position ($pos):<br>";
  54. print "$digit * $base<sup>$pos</sup> = $result
  55. <br><br>";
  56. $sums[] = $result;
  57. }
  58. print '</blockquote>';
  59. if (isset ($decimal))
  60. {
  61. print "Convert the decimal component (0.$decimal)
  62. of the number:<blockquote>";
  63. // Pad the number with a leading 0 so that we can
  64. // start at position 1
  65. $decimal = '0'.$decimal;
  66. $length = strlen ($decimal);
  67. for ($pos = 1; $pos < $length; ++$pos) {
  68. $digit = $decimal[$pos];
  69. // Handle character values for digits
  70. // (for bases greater than 10)
  71. if (eregi ('[a-z]', $digit))
  72. {
  73. $digit_value =
  74. (ord (strtolower ($digit))
  75. - ord ('a')) + 10;
  76. $digit = "$digit ($digit_value)";
  77. }
  78. else
  79. {
  80. $digit_value = $digit;
  81. }
  82. // Multiply the current digit by the radix
  83. // raised to the power of the current position
  84. $result = $digit_value * pow (1/$base, $pos);
  85. print "Multiply the value of the digit at
  86. position $pos by the value of the 1/radix
  87. ($base) raised to the power of the position
  88. ($pos):<br>";
  89. print "$digit * 1/$base<sup>$pos</sup> =
  90. $result<br><br>";
  91. $sums[] = $result;
  92. }
  93. print '</blockquote>';
  94. }
  95. $sums = implode (' + ', $sums);
  96. eval ("\$base_10_value = $sums;");
  97. print "</blockquote>The value of the base $base number
  98. $number in base 10 is $base_10_value. <br>";
  99. print "This number is derived from the sum of the values
  100. of the previous operations ($sums). <br> <br>";
  101. }

希望本文所述对大家的php程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/985264.htmlTechArticlephp实现将任意进制数转换成10进制的方法,php进制 本文实例讲述了php实现将任意进制数转换成10进制的方法。分享给大家供大家参考。具体...

人气教程排行