本文实例讲述了php实现将任意进制数转换成10进制的方法。分享给大家供大家参考。具体如下:
php将任意进制的数转换成10进制,例如8进制转换成10进制,16进制转换成10进制
<"<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程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- ABC唱片-鲍比达·新民乐《满江红》[APE+CUE]
- 许嵩.2014-不如吃茶去【海蝶】【WAV+CUE】
- 周笔畅.2024-HAVE.A.GOOD.NIGHT【SN.Music】【FLAC分轨】
- 周笔畅.2024-HAVE.A.NICE.DAY【SN.Music】【FLAC分轨】
- 证声音乐图书馆《真夏派对 x 迪斯可》[FLAC/分轨][380.78MB]
- 证声音乐图书馆《星空下 爵士钢琴》[320K/MP3][65.88MB]
- 证声音乐图书馆《星空下 爵士钢琴》[FLAC/分轨][283.95MB]
- 沙门怀一《竹山听雨HQ》头版限量[低速原抓WAV+CUE]
- 沙门怀一《于喁·壹HQ》头版限量编号[低速原抓WAV+CUE]
- 群星《魅音绝唱》黑胶CD【WAV】
- 腾格尔.2002-四十独白【风潮】【WAV+CUE】
- 陈明真.1992-到哪里找那么好的人【华星】【WAV+CUE】
- 黄凯芹.2012-廿五年3CD【环球】【WAV+CUE】
- 证声音乐图书馆《七夕 爵士情缘》[320K/MP3][64.8MB]
- 证声音乐图书馆《七夕 爵士情缘》[FLAC/分轨][327.79MB]