Binary String to Array to HEX Color - php
Trying to import some color pallets from a program which stores them in binary format. I need them to be split and converted to hex. I didn't think it would be too difficult after seeing this tool: https://www.binaryhexconverter.com/binary-to-hex-converter, but I am not getting the results I expected.
$color = "[00001010,11101111,11000001,00000000,11111111,11111111,11111111,11111111,11111111,11111111,01100000,00000000,10010000,00000000,11111111,11111111,01100000,00000000,10010000,00000000,11111111,11111111,01100000,00000000,10010000,00000000,11111111,11111111,01100000,00000000,11111111,11111111,11111111,11111111,01100000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,10001010,00000000,11000000,00000000,11111111,11111111,10001010,00000000,11000000,00000000,11111111,11111111,10001010,00000000,11000000,00000000,11111111,11111111,11000000,00000000,11111111,11111111,11111111,11111111,10010000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,01110011,01110101,01110010,01100111,01100101,01110010,01111001,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,10101110,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,10101110,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11000000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11000000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,11000000,11000000,11000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000]";
$new_arr = array_map('trim', explode(',', trim($color, '[]')));
$arr = array_chunk($new_arr, 3);
foreach($arr as $bingroup){
print("<div style='position:absolute;height:20px;width:20px;background:#");
foreach($bingroup as $binitem){
$hex = bin2hex ($binitem);
print($hex);
}
print("'></div>");
}
So I import the string, trim, explode it to an array, group them into groups of 3 and then run over it with a couple foreach loops using bin2hex() -> simple right? Why then am I getting
<div style="position:absolute;height:20px;width:20px;background:#303030303130313031313130313131313131303030303031"></div>
See, this is happening as you want
<?php
$color = "[00001010,11101111,11000001,00000000,11111111,11111111,11111111,11111111,11111111,11111111,01100000,00000000,10010000,00000000,11111111,11111111,01100000,00000000,10010000,00000000,11111111,11111111,01100000,00000000,10010000,00000000,11111111,11111111,01100000,00000000,11111111,11111111,11111111,11111111,01100000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,10001010,00000000,11000000,00000000,11111111,11111111,10001010,00000000,11000000,00000000,11111111,11111111,10001010,00000000,11000000,00000000,11111111,11111111,11000000,00000000,11111111,11111111,11111111,11111111,10010000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,01110011,01110101,01110010,01100111,01100101,01110010,01111001,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,10101110,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,10101110,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11000000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11000000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,11000000,11000000,11000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000]";
$new_arr = array_map('trim', explode(',', trim($color, '[]')));
$arr = array_chunk($new_arr, 3);
foreach($arr as $bingroup){
print("<div style='position:absolute;height:20px;width:20px;background:#");
foreach($bingroup as $binitem){
$hex = base_convert($binitem, 2, 16);
if(strlen($hex) == 1){
$hex = '0'.$hex;
}
print($hex);
}
print("'></div>");
}
This happens because you're not actually converting a binary number to hexadecimal, which bin2hex() expects. You're trying to convert a string representation of a binary number to hexadecimal.
The ASCII character "0" is "30" in hexadecimal, character "1" is "31" in hexadecimal. So converting the string "00001010" to hexadecimal using binhex() will return the string "3030303031303130".
A workaround to get what you want is to first convert the string to decimal (funnily enough, bindec() does support strings) and then go from decimal to hexadecimal:
echo dechex(bindec("00001010"));
// "a"
// -> 00001010 in binary = 10 in decimal = "a" in hexadecimal
So you need to change this line:
// $hex = bin2hex ($binitem);
$hex = dechex(bindec($binitem));
CSS either supports single character color codes ("#abc") for all colors or double for all ("#aabbcc"), but not a mixture of the two so you might also need to add an extra check to make sure color codes < 16 will be output as "0a" instead of just "a".
You can use sprintf() for this. This formats the decimal (after conversion) as hexadecimal padded with a 0 to 2 characters.
$hex = sprintf('%02x', bindec($binitem));
Related
How to convert a large binary string to a binary value and back
I need to take a large binary string (whose length will always be divisible by 8) ... // 96-digit binary string $str = '000000000011110000000000000000001111111111111111111111111111111111111111000000000000000000001111'; ... then convert it to a binary value (to store in a mysql db as type varbinary), and later convert it back again to recreate that string. This is most likely NOT a duplicate question. Every posted stackoverflow answer I could find is either broken (PHP7 apparently changed how some of these functions work) or doesn't offer a solution to this specific problem. I've tried a few things, such as ... // get binary value from binary string $bin = pack('H*', base_convert($str, 2, 16)); // get binary string from binary value $str2 = str_pad(base_convert(unpack('H*', $bin)[1], 16, 2), 96, 0, STR_PAD_LEFT); ... but this doesn't actually work. My goal is to go back and forth between the given binary string and the smallest binary value. How is this best done?
These functions convert bit strings to binary character strings and back. function binStr2charStr(string $binStr) : string { $rest8 = strlen($binStr)%8; if($rest8) { $binStr = str_repeat('0', 8 - $rest8).$binStr; } $strChar = ""; foreach(str_split($binStr,8) as $strBit8){ $strChar .= chr(bindec($strBit8)); } return $strChar; } function charStr2binStr(string $charStr) : string { $strBin = ""; foreach(str_split($charStr,1) as $char){ $strBin .= str_pad(decbin(ord($char)),8,'0', STR_PAD_LEFT); } return $strBin; } usage: // 96-digit binary string $str = '000000000011110000000000000000001111111111111111111111111111111111111111000000000000000000001111'; $strChars = binStr2charStr($str); // "\x00<\x00\x00\xff\xff\xff\xff\xff\x00\x00\x0f" //back $strBin = charStr2binStr($strChars);
how to convert binary into base64? [duplicate]
I know this is a pretty silly question, but I don't know what to do. I have an arbitrary binary number, say, 1001000000110010000000100100000010000011000000010001000001011000110000110000011100011100000011000000010010011000100000000000000100100000010110001100001000000111 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: http://home2.paulschou.net/tools/xlate/ http://convertxy.com/index.php/numberbases/ 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.
Length of a string in example is 160. It makes me think that it holds info about 160/8 characters. So, split string into parts, each part holds 8 binary digits and describes single character convert each part into a decimal integer 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. Code: 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); Result: kDICQIMBEFjDBxwMBJiAASBYwgc= 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); Result: 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. [EDIT] 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);
Convert 64 bit Integer hex string to 64 bit decimal string on 32 bit system
What is a simple way to convert a 64 bit integer encoded as a hex string to a decimal string on a 32 bit system. It needs to be the full value, it can not be in scientific notation or truncated :/ "0c80000000000063" == "900719925474099299" "0c80000000000063" != 9.007199254741E+17 PHP's base_convert() and hexdec() don't do the job right.
You need to use BC Math PHP extension (bundled). First split your input string to get high and low bytes, next convert it to decimal and then do calculation via BC functions like this: $input = "0C80000000000063"; $str_high = substr($input, 0, 8); $str_low = substr($input, 8, 8); $dec_high = hexdec($str_high); $dec_low = hexdec($str_low); //workaround for argument 0x100000000 $temp = bcmul ($dec_high, 0xffffffff); $temp2 = bcadd ($temp, $dec_high); $result = bcadd ($temp2, $dec_low); echo $result; /* 900719925474099299 */
Have you seen the first comment to hexdec's help page on php.net? When given large numbers, the hexdec function automatically converts the value to scientific notation. So, "aa1233123124121241" as a hexadecimal value will be converted to "3.13725790445E+21". If you're converting a hexadecimal value that represents a hash value (md5 or sha), then you need every single bit of that representation to make it useful. By using the number_format function, you can do that perfectly. For example : <?php // Author: holdoffhunger#gmail.com // Example Hexadecimal // --------------------------------------------- $hexadecimal_string = "1234567890abcdef1234567890abcdef"; // Converted to Decimal // --------------------------------------------- $decimal_result = hexdec($hexadecimal_string); // Print Pre-Formatted Results // --------------------------------------------- print($decimal_result); // Output Here: "2.41978572002E+37" // ..................................... // Format Results to View Whole All Digits in Integer // --------------------------------------------- // ( Note: All fractional value of the // Hexadecimal variable are ignored // in the conversion. ) $current_hashing_algorithm_decimal_result = number_format($decimal_result, 0, '', ''); // Print Formatted Results // --------------------------------------------- print($current_hashing_algorithm_decimal_result); // Output Here: "24197857200151253041252346215207534592" // ..................................... ?>
Byte array in PHP
I have a a php string: e.g. df299cc4cda279e4d7344b42a8006d94488c753f This is representing a 20 bit HEX output. How would I select the n'th byte? For example if I select the 3rd one it should return 9c. Thanks.
You can take a look at chunk_split() or str_split() and get a result like: $string = "df299cc4cda279e4d7344b42a8006d94488c753f"; $bytes = str_split($string, 2); $yourByte = $bytes[2];
$bytes = str_split($string, 2); echo $bytes[2]; Or you may want to convert the string to actual binary using hex2bin, then echo the index [2] of the string, possibly converting back to hex for output.
Given $n = byte number (3 in your example) and $data = string with hex output, return substr($data, ($n - 1) * 2, 2)
Convert a binary number to Base 64
I know this is a pretty silly question, but I don't know what to do. I have an arbitrary binary number, say, 1001000000110010000000100100000010000011000000010001000001011000110000110000011100011100000011000000010010011000100000000000000100100000010110001100001000000111 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: http://home2.paulschou.net/tools/xlate/ http://convertxy.com/index.php/numberbases/ 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.
Length of a string in example is 160. It makes me think that it holds info about 160/8 characters. So, split string into parts, each part holds 8 binary digits and describes single character convert each part into a decimal integer 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. Code: 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); Result: kDICQIMBEFjDBxwMBJiAASBYwgc= 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); Result: 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. [EDIT] 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);