-1
\$\begingroup\$

Input: a string in a representation supported by your language

Task: Output the string's representation in base64.

, 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.

\$\endgroup\$
1
  • \$\begingroup\$ Without built-in functions? \$\endgroup\$
    – u111
    Commented Nov 25 at 16:26

1 Answer 1

0
\$\begingroup\$

trivially,

Bash, 6 bytes

base64

Attempt This Online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.