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


Posted in PHP onApril 17, 2015

本文实例讲述了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版(1)
Oct 09 PHP
PHP控制网页过期时间的代码
Sep 28 PHP
php压缩多个CSS为一个css的代码并缓存
Apr 21 PHP
探讨php中防止SQL注入最好的方法是什么
Jun 10 PHP
php实现与erlang的二进制通讯实例解析
Jul 23 PHP
完美实现wordpress禁止文章修订和自动保存的方法
Nov 03 PHP
php使用指定编码导出mysql数据到csv文件的方法
Mar 31 PHP
Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
Mar 05 PHP
php array_udiff_assoc 计算两个数组的差集实例
Nov 12 PHP
ThinkPHP实现转换数据库查询结果数据到对应类型的方法
Nov 16 PHP
PHP开发之用微信远程遥控服务器
Jan 25 PHP
php实现有序数组旋转后寻找最小值方法
Sep 27 PHP
php从数据库查询结果生成树形列表的方法
Apr 17 #PHP
php实现阿拉伯数字和罗马数字相互转换的方法
Apr 17 #PHP
php实现根据词频生成tag云的方法
Apr 17 #PHP
php计算两个坐标(经度,纬度)之间距离的方法
Apr 17 #PHP
php使用GD创建保持宽高比缩略图的方法
Apr 17 #PHP
PHP中preg_match正则匹配中的/u、/i、/s含义
Apr 17 #PHP
php和editplus正则表达式去除空白行
Apr 17 #PHP
You might like
国内咖啡文化
2021/03/03 咖啡文化
基于laravel缓冲cache的用法详解
2019/10/23 PHP
php开发最强大的IDE编辑的phpstorm 2020.2配置Xdebug调试的详细教程
2020/08/17 PHP
收集的10个免费的jQuery相册
2011/02/26 Javascript
返回上一页并自动刷新的JavaScript代码
2014/02/19 Javascript
一款由jquery实现的整屏切换特效
2014/09/15 Javascript
JavaScript学习笔记之Function对象
2015/01/22 Javascript
js调出上下文菜单的实例
2015/12/17 Javascript
IE和Firefox之间在JavaScript语法上的差异
2016/04/22 Javascript
AngularJS使用自定义指令替代ng-repeat的方法
2016/09/17 Javascript
JSP防止网页刷新重复提交数据的几种方法
2016/11/19 Javascript
javascript垃圾收集机制的原理分析
2016/12/08 Javascript
electron demo项目npm install安装失败的解决方法
2018/02/06 Javascript
Element input树型下拉框的实现代码
2018/12/21 Javascript
layer.prompt输入层的例子
2019/09/24 Javascript
微信小程序实现下滑到底部自动翻页功能
2020/03/07 Javascript
[53:13]2014 DOTA2国际邀请赛中国区预选赛5.21 DT VS LGD-GAMING
2014/05/22 DOTA
django之常用命令详解
2016/06/30 Python
python模拟登陆,用session维持回话的实例
2018/12/27 Python
用Python逐行分析文件方法
2019/01/28 Python
Python实现的爬取百度文库功能示例
2019/02/16 Python
Python微信操控itchat的方法
2019/05/31 Python
Python paramiko模块使用解析(实现ssh)
2019/08/30 Python
python 实现图片上传接口开发 并生成可以访问的图片url
2019/12/18 Python
sklearn+python:线性回归案例
2020/02/24 Python
200行python代码实现贪吃蛇游戏
2020/04/24 Python
捷克厨房用品购物网站:Tescoma
2018/07/13 全球购物
全球领先的在线cosplay服装商店:RoleCosplay
2020/01/18 全球购物
检查接待方案
2014/02/27 职场文书
六个一活动实施方案
2014/03/21 职场文书
信息与工商管理职业规划范文:为梦想而搏击
2014/09/11 职场文书
群众路线批评与自我批评发言稿
2014/10/16 职场文书
销售区域经理岗位职责
2015/04/10 职场文书
无婚姻登记记录证明
2015/06/18 职场文书
狂人日记读书笔记
2015/06/30 职场文书
用基于python的appium爬取b站直播消费记录
2021/04/17 Python