php實(shí)現(xiàn)將任意進(jìn)制數(shù)轉(zhuǎn)換成10進(jìn)制的方法
本文實(shí)例講述了php實(shí)現(xiàn)將任意進(jìn)制數(shù)轉(zhuǎn)換成10進(jìn)制的方法。分享給大家供大家參考。具體如下:
php將任意進(jìn)制的數(shù)轉(zhuǎn)換成10進(jìn)制,例如8進(jìn)制轉(zhuǎn)換成10進(jìn)制,16進(jìn)制轉(zhuǎn)換成10進(jìn)制
<?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/>"; }
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP實(shí)現(xiàn)十進(jìn)制、二進(jìn)制、八進(jìn)制和十六進(jìn)制轉(zhuǎn)換相關(guān)函數(shù)用法分析
- php實(shí)現(xiàn)文件與16進(jìn)制相互轉(zhuǎn)換的方法示例
- PHP實(shí)現(xiàn)接收二進(jìn)制流轉(zhuǎn)換成圖片的方法
- PHP二進(jìn)制與字符串之間的相互轉(zhuǎn)換教程
- php圖片的二進(jìn)制轉(zhuǎn)換實(shí)現(xiàn)方法
- PHP函數(shù)篇詳解十進(jìn)制、二進(jìn)制、八進(jìn)制和十六進(jìn)制轉(zhuǎn)換函數(shù)說(shuō)明
- php實(shí)現(xiàn)36進(jìn)制與10進(jìn)制轉(zhuǎn)換功能示例
- PHP進(jìn)制轉(zhuǎn)換實(shí)例分析(2,8,16,36,64進(jìn)制至10進(jìn)制相互轉(zhuǎn)換)
- PHP實(shí)現(xiàn)的各種進(jìn)制相互轉(zhuǎn)換功能小工具示例
相關(guān)文章
PHP人民幣金額數(shù)字轉(zhuǎn)中文大寫的函數(shù)代碼
在網(wǎng)上看到一個(gè)非常有趣的PHP人民幣金額數(shù)字轉(zhuǎn)中文大寫的函數(shù),其實(shí)質(zhì)就是數(shù)字轉(zhuǎn)換成中文大寫,測(cè)試了一下,非常有趣,隨便輸個(gè)數(shù)字,就可以將其大寫打印出來(lái),新手朋友們?cè)囈幌掳?/div> 2013-02-02PHP判斷一個(gè)gif圖片是否為動(dòng)態(tài)圖片的方法
這篇文章主要介紹了PHP判斷一個(gè)gif圖片是否為動(dòng)態(tài)圖片的方法,涉及針對(duì)圖片字節(jié)流中包含數(shù)據(jù)字段的判斷,具有不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11PHP程序員最常犯的11個(gè)MySQL錯(cuò)誤小結(jié)
對(duì)于大多數(shù)web應(yīng)用來(lái)說(shuō),數(shù)據(jù)庫(kù)都是一個(gè)十分基礎(chǔ)性的部分。如果你在使用PHP,那么你很可能也在使用MySQL—LAMP系列中舉足輕重的一份子。2010-11-11php 網(wǎng)頁(yè)播放器用來(lái)播放在線視頻的代碼(自動(dòng)判斷并選擇視頻文件類型)
其實(shí)這里的php 視頻播放代碼基本上常見的視頻格式都支持,用正則匹配文件擴(kuò)展名,并根據(jù)文件擴(kuò)展名的不同調(diào)用相應(yīng)的在線播放器代碼。2010-06-06PHP簡(jiǎn)單開啟curl的方法(測(cè)試可行)
這篇文章主要介紹了PHP簡(jiǎn)單開啟curl的方法,較為詳細(xì)的講述了PHP開啟curl函數(shù)庫(kù)的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01最新評(píng)論