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 相关文章推荐
优化使用mysql存储session的php代码
Jan 10 PHP
PHP两种去掉数组重复值的方法比较
Jun 19 PHP
国产PHP开发框架myqee新手快速入门教程
Jul 14 PHP
PHP魔术引号所带来的安全问题分析
Jul 15 PHP
php实现比较两个字符串日期大小的方法
May 12 PHP
php实现的任意进制互转类分享
Jul 07 PHP
php析构函数的简单使用说明
Aug 24 PHP
php打包压缩文件之ZipArchive方法用法分析
Apr 30 PHP
使用ThinkPHP的自动完成实现无限级分类实例详解
Sep 02 PHP
php版微信自动登录并获取昵称的方法
Sep 23 PHP
PHP实现webshell扫描文件木马的方法
Jul 31 PHP
php 根据URL下载远程图片、压缩包、pdf等文件到本地
Jul 26 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对gzip文件或者字符串解压实例参考
2008/07/25 PHP
163的邮件用phpmailer发送(实例详解)
2013/06/24 PHP
PHP实现将视频转成MP4并获取视频预览图的方法
2015/03/12 PHP
PHP函数实现从一个文本字符串中提取关键字的方法
2015/07/01 PHP
PHP Socket网络操作类定义与用法示例
2017/08/30 PHP
PHP实现获取url地址中顶级域名的方法示例
2019/06/05 PHP
才发现的超链接js导致网页中GIF动画停止的解决方法
2007/11/02 Javascript
js网页侧边随页面滚动广告效果实现
2011/04/14 Javascript
用jquery写的一个万年历(自写)
2014/01/20 Javascript
js二维数组定义和初始化的三种方法总结
2014/03/03 Javascript
JavaScript中的cacheStorage使用详解
2015/07/29 Javascript
盘点javascript 正则表达式中 中括号的【坑】
2016/03/16 Javascript
无缝滚动的简单实现代码(推荐)
2016/06/07 Javascript
JavaScript随机打乱数组顺序之随机洗牌算法
2016/08/02 Javascript
微信小程序 自动登陆PHP源码实例(源码下载)
2017/05/08 Javascript
vue-cli 脚手架基于Nightwatch的端到端测试环境的过程
2018/09/30 Javascript
微信小程序五子棋游戏的棋盘,重置,对弈实现方法【附demo源码下载】
2019/02/20 Javascript
js实现内置计时器
2019/12/16 Javascript
跟老齐学Python之Python文档
2014/10/10 Python
通过数据库对Django进行删除字段和删除模型的操作
2015/07/21 Python
Python实现的微信公众号群发图片与文本消息功能实例详解
2017/06/30 Python
PyQt5每天必学之事件与信号
2018/04/20 Python
Python使用Flask-SQLAlchemy连接数据库操作示例
2018/08/31 Python
Python3爬虫学习之爬虫利器Beautiful Soup用法分析
2018/12/12 Python
python实现微信机器人: 登录微信、消息接收、自动回复功能
2019/04/29 Python
Python 求数组局部最大值的实例
2019/11/26 Python
python 爬虫网页登陆的简单实现
2020/11/30 Python
如何编写python的daemon程序
2021/01/07 Python
HTML5实现桌面通知 提示功能
2017/10/11 HTML / CSS
自荐信需注意事项
2014/01/25 职场文书
公司授权委托书
2014/04/04 职场文书
莫言诺贝尔获奖演讲稿
2014/05/21 职场文书
生物学专业求职信
2014/07/23 职场文书
学雷锋献爱心倡议书
2015/04/27 职场文书
基于Redis的List实现特价商品列表功能
2021/08/30 Redis
MYSQL 表的全面总结
2021/11/11 MySQL