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è)計(jì)模式 State (狀態(tài)模式)
允許一個(gè)對(duì)象在其內(nèi)部狀態(tài)改變時(shí)改變它的行為,對(duì)象看起來(lái)似乎修改了它所屬的類2011-06-06
一個(gè)簡(jiǎn)潔的PHP可逆加密函數(shù)(分享)
本篇文章是對(duì)一個(gè)簡(jiǎn)潔的PHP可逆加密函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php實(shí)現(xiàn)轉(zhuǎn)換html格式為文本格式的方法
這篇文章主要介紹了php實(shí)現(xiàn)轉(zhuǎn)換html格式為文本格式的方法,通過(guò)一個(gè)自定義函數(shù)實(shí)現(xiàn)針對(duì)HTML標(biāo)簽的過(guò)濾,涉及php正則替換的相關(guān)操作技巧,需要的朋友可以參考下2016-05-05
PHP 正則表達(dá)式之正則處理函數(shù)小結(jié)(preg_match,preg_match_all,preg_replace,pr
本節(jié)我們就來(lái)介紹一下PHP中基于perl的正則表達(dá)式處理函數(shù),主要包含了分割, 匹配,查找,替換等等處理操作,依舊是配合示例講解,讓我們開始吧2012-10-10
PHP循環(huán)與分支知識(shí)點(diǎn)梳理
涉及到一些比較復(fù)雜的邏輯,分支與循環(huán)是必不可少的。通過(guò)分支和循環(huán)的結(jié)合使用可以使業(yè)務(wù)更加復(fù)雜,代碼功能更加強(qiáng)大,這篇文章主要介紹了PHP循環(huán)與分支知識(shí)點(diǎn)2022-11-11

