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 强制性文件下载功能的函数代码(任意文件格式)
May 26 PHP
PHP中输出转义JavaScript代码的实现代码
Apr 22 PHP
注意:php5.4删除了session_unregister函数
Aug 05 PHP
PhpDocumentor 2安装以及生成API文档的方法
May 21 PHP
ThinkPHP3.1新特性之G方法的使用
Jun 19 PHP
19个Android常用工具类汇总
Dec 30 PHP
PHP7+Nginx的配置与安装教程详解
May 10 PHP
总结对比php中的多种序列化
Aug 28 PHP
PHP入门教程之会话控制技巧(cookie与session)
Sep 11 PHP
PHP7多线程搭建教程
Apr 21 PHP
PHP检查网站是否宕机的方法示例
Jul 24 PHP
详解PHP中的外观模式facade pattern
Feb 05 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
Yii框架多语言站点配置方法分析【中文/英文切换站点】
2020/04/07 PHP
网页设计常用的一些技巧
2006/12/22 Javascript
javascript 有趣而诡异的数组
2009/04/06 Javascript
基于jQuery的日期选择控件
2009/10/27 Javascript
JavaScript浏览器选项卡效果
2010/08/25 Javascript
jquery一般方法介绍 入门参考
2011/06/21 Javascript
jquery退出each循环的写法
2014/02/26 Javascript
js获取字符串字节数方法小结
2015/06/09 Javascript
Javascript基于AJAX回调函数传递参数实例分析
2015/12/15 Javascript
JavaScript中Window对象的属性及事件
2015/12/25 Javascript
Javascript中浏览器窗口的基本操作总结
2016/08/18 Javascript
jQuery Dialog 取消右上角删除按钮事件
2016/09/07 Javascript
ionic2 tabs使用 Modal底部tab弹出框
2016/12/30 Javascript
JS实现点击链接切换显示隐藏内容的方法
2017/10/19 Javascript
AngularJS 教程及实例代码
2017/10/23 Javascript
React Native 自定义下拉刷新上拉加载的列表的示例
2018/03/01 Javascript
vue中动态设置meta标签和title标签的方法
2018/07/11 Javascript
vue 监听键盘回车事件详解 @keyup.enter || @keyup.enter.native
2018/08/25 Javascript
Puppeteer 爬取动态生成的网页实战
2018/11/14 Javascript
Vue实现数据请求拦截
2019/10/23 Javascript
vant组件中 dialog的确认按钮的回调事件操作
2020/11/04 Javascript
vue实现登录、注册、退出、跳转等功能
2020/12/23 Vue.js
微信小程序 接入腾讯地图的两种写法
2021/01/12 Javascript
[03:46]DOTA2英雄基础教程 维萨吉
2013/12/11 DOTA
[54:29]2018DOTA2亚洲邀请赛 4.7 淘汰赛 VP vs LGD 第二场
2018/04/09 DOTA
python实现的文件夹清理程序分享
2014/11/22 Python
Python md5与sha1加密算法用法分析
2017/07/14 Python
python中的set实现不重复的排序原理
2018/01/24 Python
对pycharm 修改程序运行所需内存详解
2018/12/03 Python
python实现b站直播自动发送弹幕功能
2021/02/20 Python
师范生教师实习自我鉴定
2013/09/27 职场文书
自我鉴定书范文
2013/10/02 职场文书
公司优秀员工获奖感言
2014/08/14 职场文书
2015年双拥工作总结
2015/04/08 职场文书
小学中队委竞选稿
2015/11/20 职场文书
MySql 8.0及对应驱动包匹配的注意点说明
2021/06/23 MySQL