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 相关文章推荐
通过JavaScript或PHP检测Android设备的代码
Mar 09 PHP
php模拟post行为代码总结(POST方式不是绝对安全)
Feb 22 PHP
Could not load type System.ServiceModel.Activation.HttpModule解决办法
Dec 29 PHP
PHP实现对站点内容外部链接的过滤方法
Sep 10 PHP
php+mysqli实现将数据库中一张表信息打印到表格里的方法
Jan 28 PHP
thinkphp微信开之安全模式消息加密解密不成功的解决办法
Dec 02 PHP
CI(CodeIgniter)模型用法实例分析
Jan 20 PHP
php 微信公众平台开发模式实现多客服的实例代码
Nov 07 PHP
php strftime函数的详细用法
Jun 21 PHP
PHP利用百度ai实现文本和图片审核
May 08 PHP
laravel框架中控制器的创建和使用方法分析
Nov 23 PHP
php如何实现数据库的备份和恢复
Nov 30 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
ThinkPHP框架整合微信支付之Native 扫码支付模式二图文详解
2019/04/09 PHP
Yii框架的布局文件实例分析
2019/09/04 PHP
Jquery 1.42 checkbox 全选和反选代码
2010/03/27 Javascript
js中top、clientTop、scrollTop、offsetTop的区别 文字详细说明版
2011/01/08 Javascript
Jquery插件 easyUI属性汇总
2011/01/19 Javascript
html超链接打开窗口大小的方法
2013/03/05 Javascript
JavaScript在for循环中绑定事件解决事件参数不同的情况
2014/01/20 Javascript
javascript生成随机颜色示例代码
2014/05/05 Javascript
详解JavaScript语法对{}处理的坑爹之处
2014/06/05 Javascript
快速使用Bootstrap搭建传送带
2016/05/06 Javascript
JavaScript使用键盘输入控制实现数字验证功能
2016/08/19 Javascript
fullpage.js全屏滚动插件使用实例
2016/09/06 Javascript
js滚轮事件兼容性问题需要注意哪些
2016/11/15 Javascript
有趣的bootstrap走动进度条
2016/12/01 Javascript
Angular 4.x 动态创建表单实例
2017/04/25 Javascript
JavaScript中Hoisting详解 (变量提升与函数声明提升)
2017/08/18 Javascript
Vue组件之全局组件与局部组件的使用详解
2017/10/09 Javascript
探讨Vue.js的组件和模板
2017/10/27 Javascript
angular ng-model 无法获取值的处理方法
2018/10/02 Javascript
小程序实现长按保存图片的方法
2019/12/31 Javascript
JS FormData对象使用方法实例详解
2020/02/12 Javascript
antd日期选择器禁止选择当天之前的时间操作
2020/10/29 Javascript
8个非常实用的Vue自定义指令
2020/12/15 Vue.js
Python Sleep休眠函数使用简单实例
2015/02/02 Python
通过python实现随机交换礼物程序详解
2019/07/10 Python
详解Django定时任务模块设计与实践
2019/07/24 Python
如何真正的了解python装饰器
2020/08/14 Python
python/golang 删除链表中的元素
2020/09/14 Python
Django正则URL匹配实现流程解析
2020/11/13 Python
HTML最新标准HTML5总结(必看)
2016/06/13 HTML / CSS
Trunki英国官网:儿童坐骑式行李箱
2017/05/30 全球购物
Nobody Denim官网:购买高级女士牛仔裤
2021/03/15 全球购物
实验教师岗位职责
2014/02/13 职场文书
优秀乡村医生事迹材料
2014/05/28 职场文书
回门宴新娘答谢词
2015/09/29 职场文书
六五普法先进个人主要事迹材料
2015/11/03 职场文书