Convert a bytes array to base64 Your mission is to write a function/program that converts an array of bytes (i.e: an array of integers from 0 to 255), to base64.

Using built-in base64 encoders is not allowed.

The required base64 implementation is RFC 2045. (using "+", "/", and mandatory padding with "=")

Shortest code (in bytes) wins!

Example:

Input (int array): [99, 97, 102, 195, 169]

Output (string): Y2Fmw6k=
JavaScript, 177 187 198 characters
function(d){c=&quot;&quot;;for(a=e=b=0;a&lt;4*d.length/3;f=b&gt;&gt;2*(++a&amp;3)&amp;63,c+=String.fromCharCode(f+71-(f&lt;26?6:f&lt;52?0:f&lt;62?75:f^63?90:87)))a&amp;3^3&amp;&amp;(b=b&lt;&lt;8^d[e++]);for(;a++&amp;3;)c+=&quot;=&quot;;return c}

For adding linebreaks, \r\n, after each 76th character, add 23 characters to the code:
function(d){c=&quot;&quot;;for(a=e=b=0;a&lt;4*d.length/3;f=b&gt;&gt;2*(++a&amp;3)&amp;63,c+=String.fromCharCode(f+71-(f&lt;26?6:f&lt;52?0:f&lt;62?75:f^63?90:87))+(75==(a-1)%76?&quot;\r\n&quot;:&quot;&quot;))a&amp;3^3&amp;&amp;(b=b&lt;&lt;8^d[e++]);for(;a++&amp;3;)c+=&quot;=&quot;;return c}

Demo code:


var encode = function(d,a,e,b,c,f){c="";for(a=e=b=0;a&lt;4*d.length/3;f=b&gt;&gt;2*(++a&amp;3)&amp;63,c+=String.fromCharCode(f+71-(f&lt;26?6:f&lt;52?0:f&lt;62?75:f^63?90:87))+(75==(a-1)%76?"\r\n":""))a&amp;3^3&amp;&amp;(b=b&lt;&lt;8^d[e++]);for(;a++&amp;3;)c+="=";return c};

//OP test case
console.log(encode([99, 97, 102, 195, 169])); // outputs "Y2Fmw6k=".

//Quote from Hobbes' Leviathan:
console.log(
 encode(
  ("Man is distinguished, not only by his reason, but by this singular passion from " +
   "other animals, which is a lust of the mind, that by a perseverance of delight " +
   "in the continued and indefatigable generation of knowledge, exceeds the short " +
   "vehemence of any carnal pleasure.")
  .split('').map(function(i){return i.charCodeAt(0)})
 )
);