base_convert

(PHP 4, PHP 5, PHP 7)

base_convertConvert a number between arbitrary bases

Description

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

Returns a string containing number represented in base tobase. The base in which number is given is specified in frombase. Both frombase and tobase have 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. The case of the letters doesn't matter, i.e. number is interpreted case-insensitively.

Warning

base_convert() may lose precision on large numbers due to properties related to the internal "double" or "float" type used. Please see the Floating point numbers section in the manual for more specific information and limitations.

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 Values

number converted to base tobase

Examples

Example #1 base_convert() example

<?php
$hexadecimal 
'a37334';
echo 
base_convert($hexadecimal162);
?>

The above example will output:

101000110111001100110100

See Also

  • intval() - Get the integer value of a variable

add a note add a note

User Contributed Notes 24 notes

up
61
PHPCoder at niconet2k dot com
6 years ago
Convert an arbitrarily large number from any base to any base.

string convBase(string $numberInput, string $fromBaseInput, string $toBaseInput)
$numberInput number to convert as a string
$fromBaseInput base of the number to convert as a string
$toBaseInput base the number should be converted to as a string
examples for $fromBaseInput and $toBaseInput
'0123456789ABCDEF' for Hexadecimal (Base16)
'0123456789' for Decimal (Base10)
'01234567' for Octal (Base8)
'01' for Binary (Base2)
You can really put in whatever you want and the first character is the 0.
Examples:

<?php
convBase
('123', '0123456789', '01234567');
//Convert '123' from decimal (base10) to octal (base8).
//result: 173

convBase('70B1D707EAC2EDF4C6389F440C7294B51FFF57BB', '0123456789ABCDEF', '01');
//Convert '70B1D707EAC2EDF4C6389F440C7294B51FFF57BB' from hexadecimal (base16) to binary (base2).
//result:
//111000010110001110101110000011111101010110000101110
//110111110100110001100011100010011111010001000000110
//001110010100101001011010100011111111111110101011110
//111011

convBase('1324523453243154324542341524315432113200203012', '012345', '0123456789ABCDEF');
//Convert '1324523453243154324542341524315432113200203012' from senary (base6) to hexadecimal (base16).
//result: 1F9881BAD10454A8C23A838EF00F50

convBase('355927353784509896715106760','0123456789','Christopher');
//Convert '355927353784509896715106760' from decimal (base10) to undecimal (base11) using "Christopher" as the numbers.
//result: iihtspiphoeCrCeshhorsrrtrh

convBase('1C238Ab97132aAC84B72','0123456789aAbBcCdD', '~!@#$%^&*()');
//Convert'1C238Ab97132aAC84B72' from octodecimal (base18) using '0123456789aAbBcCdD' as the numbers to undecimal (base11) using '~!@#$%^&*()' as the numbers.
//result: !%~!!*&!~^!!&(&!~^@#@@@&

function convBase($numberInput, $fromBaseInput, $toBaseInput)
{
    if (
$fromBaseInput==$toBaseInput) return $numberInput;
   
$fromBase = str_split($fromBaseInput,1);
   
$toBase = str_split($toBaseInput,1);
   
$number = str_split($numberInput,1);
   
$fromLen=strlen($fromBaseInput);
   
$toLen=strlen($toBaseInput);
   
$numberLen=strlen($numberInput);
   
$retval='';
    if (
$toBaseInput == '0123456789')
    {
       
$retval=0;
        for (
$i = 1;$i <= $numberLen; $i++)
           
$retval = bcadd($retval, bcmul(array_search($number[$i-1], $fromBase),bcpow($fromLen,$numberLen-$i)));
        return
$retval;
    }
    if (
$fromBaseInput != '0123456789')
       
$base10=convBase($numberInput, $fromBaseInput, '0123456789');
    else
       
$base10 = $numberInput;
    if (
$base10<strlen($toBaseInput))
        return
$toBase[$base10];
    while(
$base10 != '0')
    {
       
$retval = $toBase[bcmod($base10,$toLen)].$retval;
       
$base10 = bcdiv($base10,$toLen,0);
    }
    return
$retval;
}
?>
up
33
JR
7 years ago
Short arabic2roman conveter:

<?php
function rome($N){
       
$c='IVXLCDM';
        for(
$a=5,$b=$s='';$N;$b++,$a^=7)
                for(
$o=$N%$a,$N=$N/$a^0;$o--;$s=$c[$o>2?$b+$N-($N&=-2)+$o=1:$b].$s);
        return
$s;
}
?>

And it works :)
up
23
Clifford dot ct at gmail dot com
6 years ago
If you use base_convert to convert a large (eg. 80-bit) hexadecimal to base-36, you might observe:

ABCDEF00001234567890 (hexadecimal) => 3O47RE02JZSW0KS8 (base-36) => ABCDEF00001240000000 (hexadecimal)

This is normal and is due to the loss of precision on large numbers.

I have written a string-based function using the built-in BC Math Extension which will overcome this and similar problems.

<?php

function str_baseconvert($str, $frombase=10, $tobase=36) {
   
$str = trim($str);
    if (
intval($frombase) != 10) {
       
$len = strlen($str);
       
$q = 0;
        for (
$i=0; $i<$len; $i++) {
           
$r = base_convert($str[$i], $frombase, 10);
           
$q = bcadd(bcmul($q, $frombase), $r);
        }
    }
    else
$q = $str;

    if (
intval($tobase) != 10) {
       
$s = '';
        while (
bccomp($q, '0', 0) > 0) {
           
$r = intval(bcmod($q, $tobase));
           
$s = base_convert($r, 10, $tobase) . $s;
           
$q = bcdiv($q, $tobase, 0);
        }
    }
    else
$s = $q;

    return
$s;
}

?>

Typical use-cases:
1.  Convert a large arbitrary precision number to base-36.
2.  Convert a 32-char hexadecimal UUID (128-bit) to a 25-char base-36 unique key, and vice versa.

Examples:

<?php
$b16
= 'ABCDEF00001234567890';
$b36 = str_baseconvert($b16, 16, 36);
echo (
"$b16 (hexadecimal) = $b36 (base-36) \\n");

$uuid = 'ABCDEF01234567890123456789ABCDEF';
$ukey = str_baseconvert($uuid, 16, 36);
echo (
"$uuid (hexadecimal) = $ukey (base-36) \\n");
?>

ABCDEF00001234567890 (hexadecimal) = 3o47re02jzqisvio (base-36)
ABCDEF01234567890123456789ABCDEF (hexadecimal) = a65xa07491kf5zyfpvbo76g33 (base-36)
up
11
lindsay at bitleap dot com
13 years ago
If you need to use base_convert with numbers larger then 32 bit, the following gmp implementation of base_convert should work.

<?php

/*use gmp library to convert base. gmp will convert numbers > 32bit*/
function gmp_convert($num, $base_a, $base_b)
{
        return
gmp_strval ( gmp_init($num, $base_a), $base_b );
}

?>
up
4
AdamJacobMuller at AdamJacobMuller dot com
14 years ago
<?php
function binarycodedstring2dec($binary) {
       
$len=strlen($binary);
       
$rows=($len/4)-1;
        if ((
$len%4)>0) {
               
$pad=$len+(4-($len%4));
               
$binary=str_pad($binary,$pad,"0",STR_PAD_LEFT);
               
$len=strlen($binary);
               
$rows=($len/4)-1;
        }
       
$x=0;
        for (
$x=0;$x<=$rows;$x++) {
               
$s=($x*4);
               
$bins=$binary[$s].$binary[$s+1].$binary[$s+2].$binary[$s+3];
               
$num=base_convert($bins,2,10);
                if (
$num>9) {
                        die(
"the string is not a proper binary coded decimal\n");
                } else {
                       
$res.=$num;
                }
        }
        return
$res;
}
?>

a binary coded decimal is converted by taking groups of four from a decimal string,
for example the binary coded decimal string
1000 = 8
10001000 does not = 136 but 88
so
binarycodedstring2dec(1000) = 8
binarycodedstring2dec(11100000111001)=3839
binarycodedstring2dec(100000111001)=839

i truly have no idea if this function will be useful to anyone, i simply failed a physics midterm because i didn't know this so i wrote this function to make sure i would never forget how to convert binary coded decimals
up
12
Theriault
8 years ago
If you would like to convert numbers into just the uppercase alphabet base and vice-versa (e.g. the column names in a Microsoft Windows Excel sheet..A-Z, AA-ZZ, AAA-ZZZ, ...), the following functions will do this.

<?php

/**
* Converts an integer into the alphabet base (A-Z).
*
* @param int $n This is the number to convert.
* @return string The converted number.
* @author Theriault
*
*/
function num2alpha($n) {
   
$r = '';
    for (
$i = 1; $n >= 0 && $i < 10; $i++) {
       
$r = chr(0x41 + ($n % pow(26, $i) / pow(26, $i - 1))) . $r;
       
$n -= pow(26, $i);
    }
    return
$r;
}
/**
* Converts an alphabetic string into an integer.
*
* @param int $n This is the number to convert.
* @return string The converted number.
* @author Theriault
*
*/
function alpha2num($a) {
   
$r = 0;
   
$l = strlen($a);
    for (
$i = 0; $i < $l; $i++) {
       
$r += pow(26, $i) * (ord($a[$l - $i - 1]) - 0x40);
    }
    return
$r - 1;
}

?>

Microsoft Windows Excel stops at IV (255), but this function can handle much larger. However, English words will start to form after a while and some may be offensive, so be careful.
up
8
bryan at bryanruiz dot com
7 years ago
A RFC 4648 Compliant Base32 encode.  Send me an email if you end up making this thing more efficient with bitwise operations and whatever else.

<?php

/**
* Encode in Base32 based on RFC 4648.
* Requires 20% more space than base64 
* Great for case-insensitive filesystems like Windows and URL's  (except for = char which can be excluded using the pad option for urls)
*
* @package default
* @author Bryan Ruiz
**/
class Base32 {

   private static
$map = array(
       
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', //  7
       
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
       
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23
       
'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31
       
'='  // padding char
   
);
   
   private static
$flippedMap = array(
       
'A'=>'0', 'B'=>'1', 'C'=>'2', 'D'=>'3', 'E'=>'4', 'F'=>'5', 'G'=>'6', 'H'=>'7',
       
'I'=>'8', 'J'=>'9', 'K'=>'10', 'L'=>'11', 'M'=>'12', 'N'=>'13', 'O'=>'14', 'P'=>'15',
       
'Q'=>'16', 'R'=>'17', 'S'=>'18', 'T'=>'19', 'U'=>'20', 'V'=>'21', 'W'=>'22', 'X'=>'23',
       
'Y'=>'24', 'Z'=>'25', '2'=>'26', '3'=>'27', '4'=>'28', '5'=>'29', '6'=>'30', '7'=>'31'
   
);
   
   
/**
     *    Use padding false when encoding for urls
     *
     * @return base32 encoded string
     * @author Bryan Ruiz
     **/
   
public static function encode($input, $padding = true) {
        if(empty(
$input)) return "";
       
$input = str_split($input);
       
$binaryString = "";
        for(
$i = 0; $i < count($input); $i++) {
           
$binaryString .= str_pad(base_convert(ord($input[$i]), 10, 2), 8, '0', STR_PAD_LEFT);
        }
       
$fiveBitBinaryArray = str_split($binaryString, 5);
       
$base32 = "";
       
$i=0;
        while(
$i < count($fiveBitBinaryArray)) {   
           
$base32 .= self::$map[base_convert(str_pad($fiveBitBinaryArray[$i], 5,'0'), 2, 10)];
           
$i++;
        }
        if(
$padding && ($x = strlen($binaryString) % 40) != 0) {
            if(
$x == 8) $base32 .= str_repeat(self::$map[32], 6);
            else if(
$x == 16) $base32 .= str_repeat(self::$map[32], 4);
            else if(
$x == 24) $base32 .= str_repeat(self::$map[32], 3);
            else if(
$x == 32) $base32 .= self::$map[32];
        }
        return
$base32;
    }
   
    public static function
decode($input) {
        if(empty(
$input)) return;
       
$paddingCharCount = substr_count($input, self::$map[32]);
       
$allowedValues = array(6,4,3,1,0);
        if(!
in_array($paddingCharCount, $allowedValues)) return false;
        for(
$i=0; $i<4; $i++){
            if(
$paddingCharCount == $allowedValues[$i] &&
               
substr($input, -($allowedValues[$i])) != str_repeat(self::$map[32], $allowedValues[$i])) return false;
        }
       
$input = str_replace('=','', $input);
       
$input = str_split($input);
       
$binaryString = "";
        for(
$i=0; $i < count($input); $i = $i+8) {
           
$x = "";
            if(!
in_array($input[$i], self::$map)) return false;
            for(
$j=0; $j < 8; $j++) {
               
$x .= str_pad(base_convert(@self::$flippedMap[@$input[$i + $j]], 10, 2), 5, '0', STR_PAD_LEFT);
            }
           
$eightBits = str_split($x, 8);
            for(
$z = 0; $z < count($eightBits); $z++) {
               
$binaryString .= ( ($y = chr(base_convert($eightBits[$z], 2, 10))) || ord($y) == 48 ) ? $y:"";
            }
        }
        return
$binaryString;
    }
}