当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > base_convert()函数
The base_convert() function converts a number from one base to another.
base_convert()函数的作用是:在任意进制之间转换数字。
base_convert(number,frombase,tobase) |
Parameter参数 | Description描述 |
---|---|
number | Required. Original value 必要参数。定义初始值 |
frombase | Required. Original base of number. Frombase has to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented with the letters a-z, with a meaning 10, b meaning 11 and z meaning 35. 必要参数。指定初始进制数。初始进制数的范围位于2和36之间(包括2和36);数值总位数如果大于10位,那么它将用a-z之间的字母表示,即:a表示10位,b表示11位,z表示35位数字 |
tobase | Required. The base to convert to. Tobase has to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented with the letters a-z, with a meaning 10, b meaning 11 and z meaning 35. 必要参数。指定需要转换为的进制数。初始进制数的范围位于2和36之间(包括2和36);数值总位数如果大于10位,那么它将用a-z之间的字母表示,即:a表示10位,b表示11位,z表示35位数字 |
Convert an octal number to a decimal number:
将一个八进制数字转换为十进制:
<?php $oct = "0031"; $dec = base_convert($oct,8,10); echo "$oct in octal is equal to $dec in decimal."; ?> |
The output of the code above will be:
上述代码将输出下面的结果:
0031 in octal is equal to 25 in decimal. |
Convert an octal number to a hexadecimal number:
将一个八进制数字转换为十六进制:
<?php $oct = "3648"; $hex = base_convert($oct,8,16); echo "$oct in octal is equal to $hex in hexadecimal."; ?> |
The output of the code above will be:
上述代码将输出下面的结果:
3648 in octal is equal to f4 in hexadecimal. |