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自动生成月历代码
Oct 09 PHP
PHP分页显示制作详细讲解
Oct 09 PHP
使用php统计字符串中中英文字符的个数
Jun 23 PHP
CURL状态码列表(详细)
Jun 27 PHP
基于flush()不能按顺序输出时的解决办法
Jun 29 PHP
php实现的SESSION类
Dec 02 PHP
php按单词截取字符串的方法
Apr 07 PHP
百度工程师讲PHP函数的实现原理及性能分析(一)
May 13 PHP
PHP微信开发之二维码生成类
Jun 26 PHP
nginx下安装php7+php5
Jul 31 PHP
Laravel学习教程之model validation的使用示例
Oct 23 PHP
Laravel5.5 手动分页和自定义分页样式的简单实现
Oct 15 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
有道搜索和IP138的IP的API接口(PHP应用)
2012/11/29 PHP
php实现四舍五入的方法小结
2015/03/03 PHP
PHP实现上传文件并存进数据库的方法
2015/07/16 PHP
JavaScript 嵌套函数指向this对象错误的解决方法
2010/03/15 Javascript
js控制页面控件隐藏显示的两种方法介绍
2013/10/09 Javascript
ZeroClipboard插件实现多浏览器复制功能(支持firefox、chrome、ie6)
2014/08/30 Javascript
js鼠标滑过图片震动特效的方法
2015/02/17 Javascript
jQuery实现垂直半透明手风琴特效代码分享
2015/08/21 Javascript
iscroll.js的上拉下拉刷新时无法回弹的解决方法
2016/02/18 Javascript
AngularJS 所有版本下载地址
2016/09/14 Javascript
sea.js常用的api简易文档
2016/11/15 Javascript
nodejs入门教程六:express模块用法示例
2017/04/24 NodeJs
深入理解vue-loader如何使用
2017/06/06 Javascript
nodejs中art-template模板语法的引入及冲突解决方案
2017/11/07 NodeJs
JS数组的常用10种方法详解
2020/05/08 Javascript
Element Dialog对话框的使用示例
2020/07/26 Javascript
[02:11]2016国际邀请赛中国区预选赛全程回顾
2016/07/01 DOTA
Django Admin中增加导出Excel功能过程解析
2019/09/04 Python
基于python修改srt字幕的时间轴
2020/02/03 Python
python数据库编程 ODBC方式实现通讯录
2020/03/27 Python
Jupyter notebook如何实现指定浏览器打开
2020/05/13 Python
python3.7调试的实例方法
2020/07/21 Python
python 基于wx实现音乐播放
2020/11/24 Python
怀旧收藏品和经典纪念品:Betty’s Attic
2018/08/29 全球购物
美国一家著名的手表在线折扣网站:Discount Watch Store
2020/02/24 全球购物
关于是否需要写商业计划书
2014/02/07 职场文书
幼儿园小班评语大全
2014/04/17 职场文书
学雷锋先进个人事迹
2014/05/26 职场文书
音乐幼师求职信
2014/07/09 职场文书
企业党员个人自我评价
2014/09/20 职场文书
政风行风评议工作总结
2014/10/21 职场文书
活着观后感
2015/06/03 职场文书
房产销售员2015年终工作总结
2015/10/22 职场文书
PyTorch梯度裁剪避免训练loss nan的操作
2021/05/24 Python
eclipse创建项目没有dynamic web的解决方法
2021/06/24 Java/Android
解决WINDOWS电脑开机后桌面没有任何图标
2022/04/09 数码科技