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

 更新时间:2015年04月17日 11:47:50   作者:皮蛋  
这篇文章主要介绍了php实现将任意进制数转换成10进制的方法,涉及php数制转换的相关技巧,非常具有实用价值,需要的朋友可以参考下

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

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

<?php
# Show the steps involved in converting a number 
# from any base (like octal or hex) to base 10
# See below for examples, instructions and copyright
function show_convert_to_base_10 ($number, $base)
{
 // If the number contains a decimal component
 if (strstr ($number, '.'))
 {
  // Get the integer and decimal components
  list ($integer, $decimal) = explode ('.', $number);
 }
 else
 {
  // The number is an integer
  $integer = $number;
 }
  print "<b>Convert the base $base number $number to a
  base 10 number:</b><blockquote>";
  print "Convert the integer component ($integer) of the
   number:<blockquote>";
 // Compute the value of the integer component
 // Loop through the integer digit by digit
 // Reverse the number for easier handling
 $integer = strrev ($integer);
 $length = strlen ($integer);
 for ($pos = 0; $pos < $length; ++$pos)
 {
  /*
   PHP lets you treat strings and numbers like arrays
   Specify an offset and get the character at that
   position
  */
   $digit = $integer[$pos];
  // Handle character values for digits
  // (for bases greater than 10)
  if (eregi ('[a-z]', $digit))
  {
   $digit_value =
     (ord (strtolower ($digit))
     - ord ('a')) + 10;
    $digit = "$digit ($digit_value)";
  }
  else
  {
   $digit_value = $digit;
  }
  // Multiply the current digit by the radix
  // raised to the power of the current position
  $result = $digit_value * pow ($base, $pos);
   print "Multiply the value of the digit at position
    $pos by the value of the radix ($base) raised
    to the power of the position ($pos):<br/>";
   print "$digit * $base<sup>$pos</sup> = $result
    <br/><br/>";
   $sums[] = $result;
 }
 print '</blockquote>';
 if (isset ($decimal))
 {
   print "Convert the decimal component (0.$decimal)
   of the number:<blockquote>";
  // Pad the number with a leading 0 so that we can
  // start at position 1
  $decimal = '0'.$decimal;
  $length = strlen ($decimal);
   for ($pos = 1; $pos < $length; ++$pos) {
   $digit = $decimal[$pos];
   // Handle character values for digits
   // (for bases greater than 10)
   if (eregi ('[a-z]', $digit))
   {
     $digit_value =
     (ord (strtolower ($digit))
     - ord ('a')) + 10;
      $digit = "$digit ($digit_value)";
   }
   else
   {
     $digit_value = $digit;
   }
   // Multiply the current digit by the radix
   // raised to the power of the current position
   $result = $digit_value * pow (1/$base, $pos);
    print "Multiply the value of the digit at
    position $pos by the value of the 1/radix
    ($base) raised to the power of the position
    ($pos):<br/>";
    print "$digit * 1/$base<sup>$pos</sup> =
    $result<br/><br/>";
    $sums[] = $result;
  }
  print '</blockquote>';
 }
 $sums = implode (' + ', $sums);
 eval ("\$base_10_value = $sums;");
  print "</blockquote>The value of the base $base number
  $number in base 10 is $base_10_value. <br/>";
  print "This number is derived from the sum of the values
  of the previous operations ($sums). <br/> <br/>";
}

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

相关文章

  • php查看网页源代码的方法

    php查看网页源代码的方法

    这篇文章主要介绍了php查看网页源代码的方法,涉及php读取网页文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • 为你总结一些php信息函数

    为你总结一些php信息函数

    PHP语言是一个基于函数的HTML语言,它庞大的函数库可以帮助我们实现许多功能需求。我们在这里为大家详细介绍了PHP信息函数包含的一些函数概念,需要的朋友可以参考下
    2015-10-10
  • php引用地址改变变量值的问题

    php引用地址改变变量值的问题

    看到原始值确实被修改了,发生在引用之后并被赋值之后,但被赋值之前则原始变量不会改变
    2012-03-03
  • php设计模式 State (状态模式)

    php设计模式 State (状态模式)

    允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它所属的类
    2011-06-06
  • 一个简洁的PHP可逆加密函数(分享)

    一个简洁的PHP可逆加密函数(分享)

    本篇文章是对一个简洁的PHP可逆加密函数进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php实现转换html格式为文本格式的方法

    php实现转换html格式为文本格式的方法

    这篇文章主要介绍了php实现转换html格式为文本格式的方法,通过一个自定义函数实现针对HTML标签的过滤,涉及php正则替换的相关操作技巧,需要的朋友可以参考下
    2016-05-05
  • PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)

    PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,pr

    本节我们就来介绍一下PHP中基于perl的正则表达式处理函数,主要包含了分割, 匹配,查找,替换等等处理操作,依旧是配合示例讲解,让我们开始吧
    2012-10-10
  • Wordpress php 分页代码

    Wordpress php 分页代码

    Wordpress php 分页代码,大家可以参考下。
    2009-10-10
  • PHP循环与分支知识点梳理

    PHP循环与分支知识点梳理

    涉及到一些比较复杂的逻辑,分支与循环是必不可少的。通过分支和循环的结合使用可以使业务更加复杂,代码功能更加强大,这篇文章主要介绍了PHP循环与分支知识点
    2022-11-11
  • header与缓冲区之间的深层次分析

    header与缓冲区之间的深层次分析

    实际的开发中,大家是否听说过在header之前不能有任何的实际输出。甚至有的认为header函数必须写在代码的最前面。可是你是否试验过header函数之前输出东西?下来让我们更深层次的了解一下
    2016-07-07

最新评论