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版(3)
Oct 09 PHP
java EJB 加密与解密原理的一个例子
Jan 11 PHP
php win下Socket方式发邮件类
Aug 21 PHP
php下保存远程图片到本地的办法
Aug 08 PHP
linux下删除7天前日志的代码(php+shell)
Jan 02 PHP
php class中self,parent,this的区别以及实例介绍
Apr 24 PHP
Fedora下安装php Redis扩展笔记
Sep 03 PHP
PHP获取指定月份第一天和最后一天的方法
Jul 18 PHP
解决出现SoapFault (looks like we got no XML document)的问题
Jun 24 PHP
php+ajax实现商品对比功能示例
Apr 13 PHP
php反射学习之不用new方法实例化类操作示例
Jun 14 PHP
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
Nov 25 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
Windows7下的php环境配置教程
2015/02/28 PHP
PHP实现对图片的反色处理功能【测试可用】
2018/02/01 PHP
jquery.AutoComplete.js中文修正版(支持firefox)
2010/04/09 Javascript
JavaScript全局函数使用简单说明
2011/03/11 Javascript
基于datagrid框架的查询
2013/04/08 Javascript
向左滚动文字 js代码效果
2013/08/17 Javascript
jquery如何实现锚点链接之间的平滑滚动
2013/12/02 Javascript
JQuery下拉框应用示例介绍
2014/04/23 Javascript
JavaScript函数详解
2014/11/17 Javascript
Angular和百度地图的结合实例代码
2016/10/19 Javascript
深入学习nodejs中的async模块的使用方法
2017/07/12 NodeJs
jquery实现图片跟随鼠标的实例
2017/10/17 jQuery
JS严格模式知识点总结
2018/02/27 Javascript
Vue-Router模式和钩子的用法
2018/02/28 Javascript
在Angular中使用JWT认证方法示例
2018/09/10 Javascript
使用webpack4编译并压缩ES6代码的方法示例
2019/04/24 Javascript
Flask框架的学习指南之开发环境搭建
2016/11/20 Python
django创建自定义模板处理器的实例详解
2017/08/14 Python
pandas的唯一值、值计数以及成员资格的示例
2018/07/25 Python
TensorFlow实现模型评估
2018/09/07 Python
PyQT5 emit 和 connect的用法详解
2019/12/13 Python
Python range与enumerate函数区别解析
2020/02/28 Python
作为网站管理者应当如何防范XSS
2014/08/16 面试题
艺术设计专业个人求职信范文
2013/12/11 职场文书
副护士长竞聘演讲稿
2014/04/30 职场文书
社团活动总结模板
2014/06/30 职场文书
2014院党委领导班子及其成员群众路线对照检查材料思想汇报
2014/10/04 职场文书
公务员年度考核登记表个人总结
2015/02/12 职场文书
学生会生活部工作总结2015
2015/03/31 职场文书
百年校庆宣传标语口号
2015/12/26 职场文书
2016教师廉洁从教心得体会
2016/01/13 职场文书
2016关于预防职务犯罪的心得体会
2016/01/21 职场文书
2016国庆促销广告语
2016/01/28 职场文书
再见,2019我们不负使命;你好,2020我们砥砺前行
2020/01/03 职场文书
python爬取某网站原图作为壁纸
2021/06/02 Python
Python docx库删除复制paragraph及行高设置图片插入示例
2022/07/23 Python