php实现将任意进制数转换成10进制的方法
网络编程 2021-07-05 09:09www.168986.cn编程入门
这篇文章主要介绍了php实现将任意进制数转换成10进制的方法,涉及php数制转换的相关技巧,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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 ponent if (strstr ($number, '.')) { // Get the integer and decimal ponents 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 ponent ($integer) of the number:<blockquote>"; // Compute the value of the integer ponent // 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 ponent (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程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程