Input: a string in a representation supported by your language
Task: Output the string's representation in base64.
code-golf, so fewest bytes in each language wins!
Here's a example base64 encoder in JavaScript:
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
let output = '';
let buffer = 0;
let bitsInBuffer = 0;
for (let i = 0; i < str.length; i++) {
buffer = (buffer << 8) | str.charCodeAt(i);
bitsInBuffer += 8;
while (bitsInBuffer >= 6) {
output += chars[(buffer >> (bitsInBuffer - 6)) & 0x3F];
bitsInBuffer -= 6;
}
}
if (bitsInBuffer > 0) {
output += chars[(buffer << (6 - bitsInBuffer)) & 0x3F];
}
while (output.length % 4) {
output += '=';
}
return output;
Without usage of any external libraries.