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中转义mysql语句的实现代码
Jun 24 PHP
ThinkPHP 连接Oracle数据库的详细教程[全]
Jul 16 PHP
一个好用的PHP验证码类实例分享
Dec 27 PHP
php判断文件夹是否存在不存在则创建
Apr 09 PHP
PHP+Mysql+jQuery文件下载次数统计实例讲解
Oct 10 PHP
浅谈PHP命令执行php文件需要注意的问题
Dec 16 PHP
php实现数组纵向转横向并过滤重复值的方法分析
May 29 PHP
php实现的统计字数函数定义与使用示例
Jul 26 PHP
php实现支持中文的文件下载功能示例
Aug 30 PHP
利用PHP如何统计Nginx日志的User Agent数据
Mar 06 PHP
laravel model模型定义实现开启自动管理时间created_at,updated_at
Oct 17 PHP
php设计模式之原型模式分析【星际争霸游戏案例】
Mar 23 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
苏联队长,苏联超人蝙蝠侠,这些登场的“山寨”英雄真的很严肃
2020/04/09 欧美动漫
PHP中路径问题的解决方案
2006/10/09 PHP
php 显示指定路径下的图片
2009/10/29 PHP
ThinkPHP模板判断输出Defined标签用法详解
2014/06/30 PHP
PHP中两个float(浮点数)比较实例分析
2015/09/27 PHP
php封装的单文件(图片)上传类完整实例
2016/10/18 PHP
判断JavaScript对象是否可用的最正确方法分析
2008/10/03 Javascript
javascript 事件查询综合 推荐收藏
2010/03/10 Javascript
基于jQuery的获取标签名的代码
2012/07/16 Javascript
js二维数组排序的简单示例代码
2014/01/24 Javascript
基于Jquery和CSS3制作数字时钟附源码下载(CSS3篇)
2015/11/24 Javascript
jQuery实现网页顶部固定导航效果代码
2015/12/24 Javascript
javascript拖拽应用实例(二)
2016/03/25 Javascript
JavaScript对Json的增删改属性详解
2016/06/02 Javascript
nodejs 简单实现动态html的方法
2018/05/12 NodeJs
JS实现可针对算术表达式求值的计算器功能示例
2018/09/04 Javascript
微信小程序登录态和检验注册过没的app.js写法
2019/05/22 Javascript
python爬取淘宝商品详情页数据
2018/02/23 Python
Vue的el-scrollbar实现自定义滚动
2018/05/29 Python
Python实现调用另一个路径下py文件中的函数方法总结
2018/06/07 Python
Django使用AJAX调用自己写的API接口的方法
2019/03/06 Python
在python里面运用多继承方法详解
2019/07/01 Python
安装docker-compose的两种最简方法
2019/07/30 Python
python TCP包注入方式
2020/05/05 Python
详解python爬取弹幕与数据分析
2020/11/14 Python
opencv python 对指针仪表读数识别的两种方式
2021/01/14 Python
Linux管理员面试经常问道的相关命令
2014/12/12 面试题
大二法学专业职业生涯规划范文
2014/02/12 职场文书
三八妇女节活动总结
2014/05/04 职场文书
护士优质服务演讲稿
2014/08/26 职场文书
2014年综合治理工作总结
2014/11/20 职场文书
委托书格式范文
2015/01/28 职场文书
学生个人总结范文
2015/02/15 职场文书
2019年新郎保证书3篇
2019/10/17 职场文书
服务器nginx权限被拒绝解决案例
2022/09/23 Servers
JDK8中String的intern()方法实例详细解读
2022/09/23 Java/Android