PHP base_convert() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The PHP base_convert() function converts a number from one base to another, such as binary to decimal or decimal to hexadecimal.

The number is passed as a string, and the result is also returned as a string using lowercase letters for digits above 9.

Syntax

Call base_convert() with the values you need. Start with this line.

PHP
$result = base_convert($num, $from_base, $to_base);
CallWhat it does
base_convert('ff', 16, 2)Converts hex ff to binary and returns '11111111'.
base_convert('255', 10, 16)Converts decimal 255 to hex and returns 'ff'.
base_convert('1010', 2, 10)Converts binary 1010 to decimal and returns '10'.

Both bases must be between 2 and 36. Letters a to z stand for digits 10 to 35.

Library declarations
PHP
base_convert(string $num, int $from_base, int $to_base): string

Parameters

ParameterDescription
numRequired. The number to convert, written as a string in the source base. Letters are not case sensitive.
from_baseRequired. The base of $num. It must be between 2 and 36.
to_baseRequired. The base to convert to. It must be between 2 and 36.

Return Value

Return ValueDescription
string$num written in the new base, using lowercase letters for digits above 9.

PHP Examples

Example 1 — Basic base_convert() usage

This example converts numbers between binary, decimal, hexadecimal and base 36.

PHP
<?php
echo base_convert('ff', 16, 2) . PHP_EOL;
echo base_convert('255', 10, 16) . PHP_EOL;
echo base_convert('1010', 2, 10) . PHP_EOL;
echo base_convert('zz', 36, 10) . PHP_EOL;

How It Works

  1. 'ff' in base 16 is 255, which is 11111111 in binary.
  2. Decimal 255 becomes ff in hexadecimal.
  3. Binary 1010 is 10 in decimal.
  4. In base 36, z is 35, so 'zz' equals 35 * 36 + 35 = 1295.

Example 2 — Uppercase, leading zeros and bad bases

base_convert() ignores letter case and leading zeros. A base outside 2 to 36 throws a ValueError, which we can catch.

PHP
<?php
echo base_convert('FF', 16, 10) . PHP_EOL;
echo base_convert('000ff', 16, 10) . PHP_EOL;

try {
    echo base_convert('10', 1, 10);
} catch (ValueError $e) {
    echo get_class($e) . PHP_EOL;
}

How It Works

  1. Uppercase 'FF' is treated the same as 'ff', so the result is 255.
  2. Leading zeros in '000ff' do not change the value.
  3. Base 1 is not allowed, so PHP throws a ValueError.
  4. The catch block prints the class name of the exception: ValueError.

Example 3 — Build a hex color code

Web colors use two hex digits per channel. We can use base_convert() together with str_pad() to build one.

PHP
<?php
$red = 255;
$green = 128;
$blue = 0;

$hex = '#';
foreach ([$red, $green, $blue] as $channel) {
    $hex .= str_pad(base_convert((string) $channel, 10, 16), 2, '0', STR_PAD_LEFT);
}

echo $hex . PHP_EOL;

How It Works

  1. We store the red, green and blue values as decimal numbers.
  2. base_convert() turns each one into hex: ff, 80 and 0.
  3. str_pad() adds a leading zero when needed, so 0 becomes 00.
  4. The pieces join to make the final color code #ff8000.

Frequently Asked Questions

It converts a number written as a string from one base to another, for example from binary (base 2) to hexadecimal (base 16).
Any base from 2 to 36. Digits above 9 use the letters a to z.
Numbers in other bases like ff or 1010 are made of characters, so a string is the natural type. Cast to int only when you need a decimal number.
Not always. It uses a float internally, so numbers above about 9 quadrillion can lose precision. For huge values use the GMP or BCMath extensions.
bindec() only converts binary to decimal. base_convert() can convert between any two bases from 2 to 36.

Next: bindec()

bindec() function converts a binary number, written as a string of 0s and 1s, into a decim.

bindec() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful