当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > base_convert()函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 base_convert()函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 532 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The base_convert() function converts a number from one base to another.
base_convert()函数的作用是:在任意进制之间转换数字。

Syntax
语法

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位数字


Example 1
案例1

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.


Example 2
案例2

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.

评论 (0) All

登陆 | 还没注册?