base_convert

Convert a number between arbitrary bases

Syntax

base_convert ( string $number , int $frombase , int $tobase ) : string

Parameters

number

The number to convert. Any invalid characters in number are silently ignored.

frombase

The base number is in

tobase

The base to convert number to

Return

number converted to base tobase

Examples

1

<?

$number = 16;
$frombase = 10;
$tobase = 2;

$return = base_convert($number, $frombase, $tobase);

echo $return;

?>
10000

2

<?

for($i = 0; $i < 16; ++$i)
{
    $number = $i;
    $frombase = 10;
    $tobase = 2;
    
    $return = base_convert($number, $frombase, $tobase);
    
    echo $number . ":" . $return . PHP_EOL;
}

?>
0:0
1:1
2:10
3:11
4:100
5:101
6:110
7:111
8:1000
9:1001
10:1010
11:1011
12:1100
13:1101
14:1110
15:1111

3

<?

for($i = 0; $i < 16; ++$i)
{
    $number = $i;
    $frombase = 10;
    $tobase = 8;
    
    $return = base_convert($number, $frombase, $tobase);
    
    echo $number . ":" . $return . PHP_EOL;
}

?>
0:0
1:1
2:2
3:3
4:4
5:5
6:6
7:7
8:10
9:11
10:12
11:13
12:14
13:15
14:16
15:17

4

<?

for($i = 0; $i < 16; ++$i)
{
    $number = $i;
    $frombase = 10;
    $tobase = 16;
    
    $return = base_convert($number, $frombase, $tobase);
    
    echo $number . ":" . $return . PHP_EOL;
}

?>
0:0
1:1
2:2
3:3
4:4
5:5
6:6
7:7
8:8
9:9
10:a
11:b
12:c
13:d
14:e
15:f

abs

acos

acosh

asin

asinh

atan

atan2

atanh

bindec

ceil

cos

cosh

decbin

dechex

decoct

deg2rad

exp

expm1

fdiv

floor

fmod

hexdec

hypot

intdiv

is_finite

is_infinite

is_nan

log

log10

log1p

max

min

octdec

pi

pow

rad2deg

round

sin

sinh

sqrt

tan

tanh

PHP

Home Menu