Home » Php » php – Convert a binary number to Base 64

php – Convert a binary number to Base 64

Posted by: admin February 25, 2020 Leave a comment


I know this is a pretty silly question, but I don’t know what to do.

I have an arbitrary binary number, say,


I want to convert it to Base 64 using PHP – and every way I try gives me a different result. Even different online converters convert it differently:


PHP’s base_convert only works up to base36, and base64_encode expects a string.

What do I do?

UPDATE: I implemented the solution functions suggested by @binaryLV, and it did work well.

However, I compared the results to PHP’s built-in base_convert. It turned out that base_convert to base36 returns shorter values that the custom base64 function! (And yes, I did prepend a ‘1’ to all the binary numbers to ensure leading zeros aren’t lost).

I have noticed, too, that base_convert is quite innacurate with large numbers. So I need is a function which works like base_convert, but accurately and, preferably, up to base 64.

How to&Answers:

Length of a string in example is 160. It makes me think that it holds info about 160/8 characters. So,

  1. split string into parts, each part holds 8 binary digits and describes single character
  2. convert each part into a decimal integer
  3. build a string from characters, that are made from ASCII codes from 2nd step

This will work with strings with size n*8. For other strings (e.g., 12 binary digits) it will give unexpected results.


function bin2base64($bin) {
    $arr = str_split($bin, 8);
    $str = '';
    foreach ( $arr as $binNumber ) {
        $str .= chr(bindec($binNumber));
    return base64_encode($str);

$bin = '1001000000110010000000100100000010000011000000010001000001011000110000110000011100011100000011000000010010011000100000000000000100100000010110001100001000000111';
echo bin2base64($bin);



Here’s also function for decoding it back to string of binary digits:

function base64bin($str) {
    $result = '';
    $str = base64_decode($str);
    $len = strlen($str);
    for ( $n = 0; $n < $len; $n++ ) {
        $result .= str_pad(decbin(ord($str[$n])), 8, '0', STR_PAD_LEFT);
    return $result;

var_dump(base64bin(bin2base64($bin)) === $bin);


boolean true


PHP has a built in base 64 encoding function, see documentation here. If you want the decimal value of the binary string first use bin2dec, there are similar functions for hexadecimals by the way. The documentation is your friend here.


I might have misunderstood your question, if you want to convert between actual bases (base 2 and 64) use base_convert


$number = 1001000000110010000000100100000010000011000000010001000001011000110000110000011100011100000011000000010010011000100000000000000100100000010110001100001000000111;
echo base64_encode ($number);

This is if you want the exact string be converted into Base 64.


To convert a binary number (2 base) to a 64 base use the base_convert function.

$number = 1001000000110010000000100100000010000011000000010001000001011000110000110000011100011100000011000000010010011000100000000000000100100000010110001100001000000111;
base_convert ($number , 2, 64);