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 批量更新网页内容实现代码
Jan 05 PHP
基于asp+ajax和数据库驱动的二级联动菜单
May 06 PHP
为PHP初学者的8点有效建议
Nov 20 PHP
PHP Undefined index报错的修复方法
Jul 17 PHP
Fine Uploader文件上传组件应用介绍
Jan 06 PHP
php实现插入排序
Mar 29 PHP
Yii2 如何在modules中添加验证码的方法
Jun 19 PHP
thinkphp3.2实现在线留言提交验证码功能
Jul 19 PHP
PHP重置数组为连续数字索引的几种方式总结
Mar 12 PHP
php用户名的密码加密更安全的方法
Jun 21 PHP
Laravel5.1 框架控制器基础用法实例分析
Jan 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+mysql一个名片库程序
2006/10/09 PHP
PHP文件读写操作之文件读取方法详解
2011/01/13 PHP
在windows平台上构建自己的PHP实现方法(仅适用于php5.2)
2013/07/05 PHP
php除数取整示例
2014/04/24 PHP
THINKPHP2.0到3.0有哪些改进之处
2015/01/04 PHP
PHP中的session安全吗?
2016/01/22 PHP
PHP清除缓存的几种方法总结
2017/09/12 PHP
Javascript Math ceil()、floor()、round()三个函数的区别
2010/03/09 Javascript
腾讯与新浪的通过IP地址获取当前地理位置(省份)的接口
2010/07/26 Javascript
JS控制div跳转到指定的位置的几种解决方案总结
2016/11/05 Javascript
微信小程序 标签传入数据
2017/05/08 Javascript
ES6新特性之类(Class)和继承(Extends)相关概念与用法分析
2017/05/24 Javascript
Angular 通过注入 $location 获取与修改当前页面URL的实例
2017/05/31 Javascript
vue引入swiper插件的使用实例
2017/07/19 Javascript
基于vue中css预加载使用sass的配置方式详解
2018/03/13 Javascript
AngularJS日期格式化常见操作实例分析
2018/05/17 Javascript
详解JSON Web Token 入门教程
2018/07/30 Javascript
浅谈对于react-thunk中间件的简单理解
2019/05/01 Javascript
OpenLayer学习之自定义测量控件
2020/09/28 Javascript
详解如何用django实现redirect的几种方法总结
2018/11/22 Python
windows7 32、64位下python爬虫框架scrapy环境的搭建方法
2018/11/29 Python
python中的单引号双引号区别知识点总结
2019/06/23 Python
Python+threading模块对单个接口进行并发测试
2019/06/25 Python
Python 硬币兑换问题
2019/07/29 Python
Django1.11配合uni-app发起微信支付的实现
2019/10/12 Python
通过Python扫描代码关键字并进行预警的实现方法
2020/05/24 Python
美国传奇滑手Paul Rodriguez创办的街头滑板品牌:Primitive Skateboarding
2019/10/29 全球购物
元旦获奖感言
2014/03/08 职场文书
简历中个人自我评价分享
2014/03/15 职场文书
师德师风演讲稿
2014/05/05 职场文书
毕业设计说明书
2014/05/07 职场文书
幼儿老师求职信
2014/06/30 职场文书
2015年远程教育工作总结
2015/05/20 职场文书
深入详解JS函数的柯里化
2021/06/09 Javascript
关于对TypeScript泛型参数的默认值理解
2022/07/15 Javascript
ubuntu如何搭建vsftpd服务器
2022/12/24 Servers