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+jquery编码方面的一些心得(utf-8 gb2312)
Oct 12 PHP
使用Sphinx对索引进行搜索
Jun 25 PHP
在PHP中使用redis
Nov 04 PHP
php配合jquery实现增删操作具体实例
Dec 12 PHP
PHP获取短链接跳转后的真实地址和响应头信息的方法
Jul 25 PHP
PHP常用技术文之文件操作和目录操作总结
Sep 27 PHP
thinkphp特殊标签用法概述
Nov 24 PHP
php将csv文件导入到mysql数据库的方法
Dec 24 PHP
CI框架AR数据库操作常用函数总结
Nov 21 PHP
PHP实现的抓取小说网站内容功能示例
Jun 27 PHP
laravel 判断查询数据库返回值的例子
Oct 11 PHP
php中Swoole的热更新实现代码实例
Mar 04 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
php过滤所有恶意字符(批量过滤post,get敏感数据)
2014/03/18 PHP
php获取CSS文件中图片地址并下载到本地的方法
2014/12/02 PHP
Yii2组件之多图上传插件FileInput的详细使用教程
2016/06/20 PHP
PHP删除字符串中非字母数字字符方法总结
2019/01/20 PHP
PHP判断访客是否手机端(移动端浏览器)访问的方法总结【4种方法】
2019/03/27 PHP
Javascript 陷阱 window全局对象
2008/11/26 Javascript
Javascript 表单之间的数据传递代码
2008/12/04 Javascript
cnblogs TagCloud基于jquery的实现代码
2010/06/11 Javascript
chrome原生方法之数组
2011/11/30 Javascript
用Javascript获取页面元素的具体位置
2013/12/09 Javascript
jquery 操作两个select实现值之间的互相传递
2014/03/07 Javascript
用JavaScript实现用一个DIV来包装文本元素节点
2014/09/09 Javascript
js使用onmousemove和onmouseout获取鼠标坐标的方法
2015/03/31 Javascript
ajax读取数据后使用jqchart显示图表的方法
2015/06/10 Javascript
JavaScript实现倒计时代码段Item1(非常实用)
2015/11/03 Javascript
js正则表达式replace替换变量方法
2016/05/21 Javascript
Vue 2.0入门基础知识之内部指令详解
2017/10/15 Javascript
浅谈Postman解决token传参的问题
2018/03/31 Javascript
使用微信SDK自定义分享的方法
2019/07/03 Javascript
Vue路由前后端设计总结
2019/08/06 Javascript
微信小程序pinker组件使用实现自动相减日期
2020/05/07 Javascript
python正则分组的应用
2013/11/10 Python
简单总结Python中序列与字典的相同和不同之处
2016/01/19 Python
Flask之flask-script模块使用
2018/07/26 Python
python遍历文件夹找出文件夹后缀为py的文件方法
2018/10/21 Python
Django中使用MySQL5.5的教程
2019/12/18 Python
Python3 mmap内存映射文件示例解析
2020/03/23 Python
python+opencv3.4.0 实现HOG+SVM行人检测的示例代码
2021/01/28 Python
Html5游戏开发之乒乓Ping Pong游戏示例(一)
2013/01/21 HTML / CSS
什么是数据抽象
2016/11/26 面试题
2014年医院科室工作总结
2014/12/20 职场文书
2015年端午节活动方案
2015/05/05 职场文书
任命书格式范文
2015/09/22 职场文书
html5 录制mp3音频支持采样率和比特率设置
2021/07/15 Javascript
springboot集成springCloud中gateway时启动报错的解决
2021/07/16 Java/Android
Python批量解压&压缩文件夹的示例代码
2022/04/04 Python