How to convert integer to byte array in php - php
how would I convert an integer to an array of 4 bytes?
Here is the exact code I want to port (in C#)
int i = 123456;
byte[] ar = BitConverter.GetBytes(i);
// ar will contain {64, 226, 1, 0}
How would I do the exact same thing in PHP ?
The equivalent conversion is
$i = 123456;
$ar = unpack("C*", pack("L", $i));
See it in action.
You should be aware though that the byte order (little/big endian) is dependent on the machine architecture (as it is also in the case of BitConverter). That might or might not be good.
Since the equivalent of a byte array in PHP is a string, this'll do:
$bytes = pack('L', 123456);
To visualize that, use bin2hex:
echo bin2hex($bytes);
// 40e20100
// (meaning 64, 226, 1, 0)
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);
Binary String to Array to HEX Color
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));
Workaround needed, PHP dechex maximum integer [duplicate]
I have some large HEX values that I want to display as regular numbers, I was using hexdec() to convert to float, and I found a function on PHP.net to convert that to decimal, but it seems to hit a ceiling, e.g.: $h = 'D5CE3E462533364B'; $f = hexdec($h); echo $f .' = '. Exp_to_dec($f); Output: 1.5406319846274E+19 = 15406319846274000000 Result from calc.exe = 15406319846273791563 Is there another method to convert large hex values?
As said on the hexdec manual page: The function can now convert values that are to big for the platforms integer type, it will return the value as float instead in that case. If you want to get some kind of big integer (not float), you'll need it stored inside a string. This might be possible using BC Math functions. For instance, if you look in the comments of the hexdec manual page, you'll find this note If you adapt that function a bit, to avoid a notice, you'll get: function bchexdec($hex) { $dec = 0; $len = strlen($hex); for ($i = 1; $i <= $len; $i++) { $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); } return $dec; } (This function has been copied from the note I linked to; and only a bit adapted by me) And using it on your number: $h = 'D5CE3E462533364B'; $f = bchexdec($h); var_dump($f); The output will be: string '15406319846273791563' (length=20) So, not the kind of big float you had ; and seems OK with what you are expecting: Result from calc.exe = 15406319846273791563 Hope this help ;-) And, yes, user notes on the PHP documentation are sometimes a real gold mine ;-)
hexdec() switches from int to float when the result is too large to be represented as an int. If you want arbitrarily long values, you're probably going to have to roll your own conversion function to change the hex string to a GMP integer. function gmp_hexdec($n) { $gmp = gmp_init(0); $mult = gmp_init(1); for ($i=strlen($n)-1;$i>=0;$i--,$mult=gmp_mul($mult, 16)) { $gmp = gmp_add($gmp, gmp_mul($mult, hexdec($n[$i]))); } return $gmp; } print gmp_strval(gmp_hexdec("D5CE3E462533364B")); Output: 15406319846273791563
$num = gmp_init( '0xD5CE3E462533364B' ); // way to input a number in gmp echo gmp_strval($num, 10); // display value in decimal That's the module to use. Convert it to a function and then use on your numbers. Note: provide these hex numbers as strings so: $num = "0x348726837469972346"; // set variable $gmpnum = gmp_init("$num"); // gmp number format echo gmp_strval($gmpnum, 10); // convert to decimal and print out
1.5406319846274E+19 is a limited representation of you number. You can have a more complete one by using printf() printf("%u\n", hexdec($h)); ...will output "15406319846273792000". PHP uses floats for such big numbers, so you may lose a bit of precision. If you have to work with arbitrary precision numbers, you may try the bcmath extension. By splitting the hex into two 32-bit words (which should be safe on most systems) you should be able to get more precision. For instance: $f = bcadd(bcmul(hexdec(substr($h, 0, -8)), 0x100000000), hexdec(substr($h, 8))); ...would set $f to 15406319846273791563.
Convert HEX to DEC is easy.. But, reconstruct back hexadecimal number is very hard. Try to use base_convert .. $hexadecimal = base_convert(2826896153644826, 10, 16); // result: a0b0c0d0e0f1a
Run into this issue while storing 64-bit keys in MySQL database. I was able to get a bit perfect conversion to a 64-bit signed integer (PHP limitation) using a few binary operators: (This code is 16x faster than bchexdec function and resulting variables are using half the memory on average). function x64toSignedInt($k){ $left = hexdec(substr($k,0,8)); $right = hexdec(substr($k,8,8)); return (int) ($left << 32) | $right; } MySQL signed BIGINT datatype is a great match for this as an index or storage in general. HEX(column) is a simple way to convert it back to HEX within the SQL query for use elsewhere.
This solution also uses the BC Math Functions. However, an algorithm is used which does without the bcpow function. This function is a bit shorter and faster than the accepted solution, tested on PHP 7.4. function hexDecBc(string $hex) : string { for ($dec = '0', $i = 0; $i < strlen($hex); $i++) { $dec = bcadd(bcmul($dec,'16'),(string)hexdec($hex[$i])); } return $dec; }
Make sure to enable gmp extension. ext-gmp $number = gmp_strval(gmp_init('0x03....')); // outputs: 1234324....
Doesn't intval(var, base) take care of it? From the PHP Manual.
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);