PHP base_convert() Function

Definition and Usage

The base_convert() function converts numbers between any base.

Syntax

base_convert(number,frombase,tobase)
Parameter Description
number Required. The original value.
frombase Required. The original base of the number.
tobase Required. The base to convert to.

Description

returns a string containing number by tobase of the number base representation.number The base of the number itself is frombase specified.frombase and tobase Can only be between 2 and 36 (including 2 and 36). Numbers higher than decimal are represented by letters a-z, such as a for 10, b for 11, and z for 35.

Example

Example 1

Convert octal number to decimal number:

<?php
$oct = "0031";
$dec = base_convert($oct,8,10);
echo "Ang 0031 sa walong-sibol ay katumbas ng 25 sa decimal.";
?>

Output:

Ang 0031 sa walong-sibol ay katumbas ng 25 sa decimal.

Example 2

Convert octal number to hexadecimal number:

<?php
$oct = "364";
$hex = base_convert($oct,8,16);
echo "Ang 364 sa walong-sibol ay katumbas ng labingwalong-sibol na $hex.";
?>

Output:

Ang 364 sa walong-sibol ay katumbas ng f4 sa labingwalong-sibol.