PHP实现8进制转10进制,php实现将任意进制数转换成10进制的方法,php进制_PHP教程...

php实现将任意进制数转换成10进制的方法,php进制

本文实例讲述了php实现将任意进制数转换成10进制的方法。分享给大家供大家参考。具体如下:

php将任意进制的数转换成10进制,例如8进制转换成10进制,16进制转换成10进制

Convert the base $base number $number to a

base 10 number:";

print "Convert the integer component ($integer) of the

number:";

// 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):

";

print "$digit * $base$pos = $result

";

$sums[] = $result;

}

print '';

if (isset ($decimal))

{

print "Convert the decimal component (0.$decimal)

of the number:";

// 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):

";

print "$digit * 1/$base$pos =

$result

";

$sums[] = $result;

}

print '';

}

$sums = implode (' + ', $sums);

eval ("\$base_10_value = $sums;");

print "The value of the base $base number

$number in base 10 is $base_10_value.

";

print "This number is derived from the sum of the values

of the previous operations ($sums).

";

}

希望本文所述对大家的php程序设计有所帮助。

http://www.bkjia.com/PHPjc/985264.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/985264.htmlTechArticlephp实现将任意进制数转换成10进制的方法,php进制 本文实例讲述了php实现将任意进制数转换成10进制的方法。分享给大家供大家参考。具体...

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值