PHP base_convert() 函数
PHP base_convert() 函数是数学函数。它用于将一个数字从一种进制转换为另一种进制。
语法
base_convert(number,frombase,tobase);
参数 | 描述 | 必需/可选 |
---|---|---|
number | 指定要转换的数字。 | 必需 |
frombase | 指定数字的原始进制。 | 必需 |
tobase | 指定要转换到的进制。 | 必需 |
示例1
将一个十六进制数转换为八进制数:
<?php
hexadecimal='a37334';
echo "Your hexadecimal number is : ".hexadecimal;
echo "<br>"."By using 'base_convert()' function your Octal number is :".base_convert($hexadecimal, 16, 2);
?>
输出:
Your number is : a37334
By using 'base_convert()' function your Octal number is :101000110111001100110100
示例2
将一个十六进制数转换为八进制数:
<?php
hexadecimal='E196';
echo "Your hexadecimal number is : ".hexadecimal;
echo "<br>"."By using 'base_convert()' function your Octal number is :".base_convert($hexadecimal, 16, 8);
?>
输出:
Your number is : E196
By using 'base_convert()' function your Octal number is :160626
示例3
将一个八进制数转换为十进制数:
<?php
oct='0031';
echo "Your Octal number is : ".oct;
echo "<br>"."By using 'base_convert()' function your Decimal number is :".base_convert($oct,8, 10);
?>
输出:
Your Octal number is : 0031
By using 'base_convert()' function your Decimal number is :25
示例4
将一个八进制数转换为十六进制数。
<?php
oct = "364";
echo "Your Octal number is : ".oct;
echo "<br>"."By using 'base_convert()' function your Hexadecimal number is :".base_convert($oct,8, 16);
?>
输出:
Your Octal number is : 364
By using 'base_convert()' function your Hexadecimal number is :f4
示例5
将二进制转换为十六进制:
<?php
binary='110011';
echo "Your binary number is : ".binary;
echo "<br>"."By using 'base_convert()' function your hexadecimal number is :".base_convert($binary,2, 16);
?>
输出:
Your binary number is : 110011
By using 'base_convert()' function your hexadecimal number is :33