10
\$\begingroup\$

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=

\$\endgroup\$
  • \$\begingroup\$ What type of competition is this? \$\endgroup\$ – Cilan May 4 '14 at 22:26
  • \$\begingroup\$ Does built-in base64 encoders cover only binary-to-text encoders or functions manipulating integers as well? \$\endgroup\$ – Dennis May 5 '14 at 4:11
  • 1
    \$\begingroup\$ To clarify: Can I use a function that returns 1 2 for the argument 66? \$\endgroup\$ – Dennis May 5 '14 at 5:21
  • 1
    \$\begingroup\$ There are 9 standardised or 4 non-standardised versions of base64. Your reference to = for padding narrows it down to 4. Which one do you want? Or do you want a non-standard variant which doesn't have maximum line lengths? \$\endgroup\$ – Peter Taylor May 5 '14 at 6:43
  • \$\begingroup\$ I'm guessing he/she referred to the either the "standard" one specified by RFC 4648 or the version used by MIME-types, RFC 2045. These are different, so clarification would be very useful. \$\endgroup\$ – semi-extrinsic May 5 '14 at 7:38

10 Answers 10

3
\$\begingroup\$

32-bit x86 assembly, 59 bytes

Byte-code:

66 B8 0D 0A 66 AB 6A 14 5A 4A 74 F4 AD 4E 45 0F C8 6A 04 59 C1 C0 06 24 3F 3C 3E 72 05 C0
E0 02 2C 0E 2C 04 3C 30 7D 08 04 45 3C 5A 76 02 04 06 AA 4D E0 E0 75 D3 B0 3D F3 AA C3

Disassembly:

b64_newline:
    mov     ax, 0a0dh
    stosw
b64encode:
    push    (76 shr 2) + 1
    pop     edx
b64_outer:
    dec     edx
    je      b64_newline
    lodsd
    dec     esi
    inc     ebp
    bswap   eax
    push    4
    pop     ecx
b64_inner:
    rol     eax, 6
    and     al, 3fh
    cmp     al, 3eh
    jb      b64_testchar
    shl     al, 2     ;'+' and '/' differ by only 1 bit
    sub     al, ((3eh shl 2) + 'A' - '+') and 0ffh
b64_testchar:
    sub     al, 4
    cmp     al, '0'
    jnl     b64_store ;l not b because '/' is still < 0 here
    add     al, 'A' + 4
    cmp     al, 'Z'
    jbe     b64_store
    add     al, 'a' - 'Z' - 1
b64_store:
    stosb
    dec     ebp
    loopne  b64_inner
    jne     b64_outer
    mov     al, '='
    rep     stosb
    ret

Call b64encode with esi pointing to input buffer, edi pointing to output buffer.

It could be made even smaller if line-wrapping is not used.

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 177 187 198 characters

function(d){c="";for(a=e=b=0;a<4*d.length/3;f=b>>2*(++a&3)&63,c+=String.fromCharCode(f+71-(f<26?6:f<52?0:f<62?75:f^63?90:87)))a&3^3&&(b=b<<8^d[e++]);for(;a++&3;)c+="=";return c}

For adding linebreaks, \r\n, after each 76th character, add 23 characters to the code:

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

Demo code:

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

\$\endgroup\$
  • \$\begingroup\$ Nice solution! You can shave off some bytes using some ES6 features and removing some duplication: Shortened code with comments \$\endgroup\$ – Craig Ayre Jun 11 '17 at 14:02
  • \$\begingroup\$ @CraigAyre, thanks for constructive input. ES6 was not finalized and available at the time this challenge was originally posted. As suggested at codegolf.meta, you could post the shortened ES6 version and mark it as non-competing. \$\endgroup\$ – Tomas Langkaas Jun 11 '17 at 21:35
  • \$\begingroup\$ No worries, my fault for not double checking the original post date! I'm a fan of your solution so I'm not going to post another, but thanks for the link. The template literal logic that removed the alphabet duplication can be converted to ES5 in the same number of bytes, doesn't save many but every little counts! \$\endgroup\$ – Craig Ayre Jun 11 '17 at 21:56
  • \$\begingroup\$ @CraigAyre, thanks again for the tip, found another way to compress the base64 symbols even more (which made it even more backwards compatible--should now work in old IE as well). \$\endgroup\$ – Tomas Langkaas Jun 11 '17 at 23:39
1
\$\begingroup\$

perl, 126 bytes

reads stdin, outputs to stdout

$/=$\;print map{$l=y///c/2%3;[A..Z,a..z,0..9,"+","/"]->[oct"0b".substr$_.0 x4,0,6],$l?"="x(3-$l):""}unpack("B*",<>)=~/.{1,6}/g

ungolfed:

my @x = ('A'..'Z','a'..'z',0..9,'+','/');
my $in = join '', <>;
my $bits = unpack 'B*', $in;
my @six_bit_groups = $bits =~ /.{1,6}/g;
for my $sixbits (@six_bit_groups) {
  next unless defined $sixbits;
  $l=length($sixbits)/2%3;
  my $zero_padded = $sixbits . ( "0" x 4 );
  my $padded_bits = substr( $zero_padded, 0, 6 );
  my $six_bit_int = oct "0b" . $padded_bits;
  print $x[$six_bit_int];
  print "=" x (3 - $l)  if  $l;
}
\$\endgroup\$
  • \$\begingroup\$ The question has been clarified to require RFC 2045, so you need to add a bit of code to split the output into 76-char chunks and join with \r\n. \$\endgroup\$ – Peter Taylor May 5 '14 at 9:34
1
\$\begingroup\$

Perl, 147 bytes

sub b{$f=(3-($#_+1)%3)%3;$_=unpack'B*',pack'C*',@_;@r=map{(A..Z,a..z,0..9,'+','/')[oct"0b$_"]}/.{1,6}/g;$"='';join"\r\n",("@r".'='x$f)=~/.{1,76}/g}

The function takes a list of integers as input and outputs the string, base64 encoded.

Example:

print b(99, 97, 102, 195, 169)

prints

Y2Fmw6kA

Ungolfed:

Version that also visualizes the intermediate steps:

sub b {
    # input array: @_
    # number of elements: $#_ + 1 ($#_ is zero-based index of last element in 
    $fillbytes = (3 - ($#_ + 1) % 3) % 3;
      # calculate the number for the needed fill bytes
      print "fillbytes:       $fillbytes\n";
    $byte_string = pack 'C*', @_;
      # the numbers are packed as octets to a binary string
      # (binary string not printed)
    $bit_string = unpack 'B*', $byte_string;
      # the binary string is converted to its bit representation, a string wit
      print "bit string:      \"$bit_string\"\n";
    @six_bit_strings = $bit_string =~ /.{1,6}/g;
      # group in blocks of 6 bit
      print "6-bit strings:   [@six_bit_strings]\n";
    @index_positions = map { oct"0b$_" } @six_bit_strings;
      # convert bit string to number
      print "index positions: [@index_positions]\n";
    @alphabet = (A..Z,a..z,0..9,'+','/');
      # the alphabet for base64
    @output_chars = map { $alphabet[$_] } @index_positions;
      # output characters with wrong last characters that entirely derived fro
      print "output chars:    [@output_chars]\n";
    local $" = ''; #"
    $output_string = "@output_chars";
      # array to string without space between elements ($")
      print "output string:   \"$output_string\"\n";
    $result = $output_string .= '=' x $fillbytes;
      # add padding with trailing '=' characters
      print "result:          \"$result\"\n";
    $formatted_result = join "\r\n", $result =~ /.{1,76}/g;
      # maximum line length is 76 and line ends are "\r\n" according to RFC 2045
      print "formatted result:\n$formatted_result\n";
    return $formatted_result;
}

Output:

fillbytes:       1
bit string:      "0110001101100001011001101100001110101001"
6-bit strings:   [011000 110110 000101 100110 110000 111010 1001]
index positions: [24 54 5 38 48 58 9]
output chars:    [Y 2 F m w 6 J]
output string:   "Y2Fmw6J"
result:          "Y2Fmw6J="
formatted result:
Y2Fmw6J=

Tests:

The test strings come from the example in the question the examples in the Wikipedia article for Base64.

sub b{$f=(3-($#_+1)%3)%3;$_=unpack'B*',pack'C*',@_;@r=map{(A..Z,a..z,0..9,'+','/')[oct"0b$_"]}/.{1,6}/g;$"='';join"\r\n",("@r".'='x$f)=~/.{1,76}/g}

sub test ($) {
   print b(map {ord($_)} $_[0] =~ /./sg), "\n\n";
}

my $str = <<'END_STR';
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.
END_STR
chomp $str;

test "\143\141\146\303\251";
test $str;
test "any carnal pleasure.";
test "any carnal pleasure";
test "any carnal pleasur";
test "any carnal pleasu";
test "any carnal pleas";
test "pleasure.";
test "leasure.";
test "easure.";
test "asure.";
test "sure.";

Test output:

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbQpvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodAppbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydAp2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZSO=

YW55IGNhcm5hbCBwbGVhc3VyZSO=

YW55IGNhcm5hbCBwbGVhc3VyZB==

YW55IGNhcm5hbCBwbGVhc3Vy

YW55IGNhcm5hbCBwbGVhc3F=

YW55IGNhcm5hbCBwbGVhcD==

cGxlYXN1cmUu

bGVhc3VyZSO=

ZWFzdXJlLC==

YXN1cmUu

c3VyZSO=
\$\endgroup\$
  • \$\begingroup\$ The question has been clarified to require RFC 2045, so you need to add a bit of code to split the output into 76-char chunks and join with \r\n. \$\endgroup\$ – Peter Taylor May 5 '14 at 9:37
  • \$\begingroup\$ @PeterTaylor: Thanks, I have updated the answer for RFC 2045. \$\endgroup\$ – Heiko Oberdiek May 5 '14 at 11:17
  • \$\begingroup\$ bravo for this very complete answer. Including mandatory line-breaks (by specifying "RFC 2045" in the OP) was actually an error, you can in fact ignore that part. Sorry :) \$\endgroup\$ – xem May 5 '14 at 18:15
1
\$\begingroup\$

Python, 234 chars

def F(s):
 R=range;A=R(65,91)+R(97,123)+R(48,58)+[43,47];n=len(s);s+=[0,0];r='';i=0
 while i<n:
  if i%57<1:r+='\r\n'
  for j in R(4):r+=chr(A[s[i]*65536+s[i+1]*256+s[i+2]>>18-6*j&63])
  i+=3
 k=-n%3
 if k:r=r[:-k]+'='*k
 return r[2:]
\$\endgroup\$
  • \$\begingroup\$ The question has been clarified to require RFC 2045, so you need to add a bit of code to split the output into 76-char chunks and join with \r\n. \$\endgroup\$ – Peter Taylor May 5 '14 at 9:38
  • \$\begingroup\$ @PeterTaylor: fixed. \$\endgroup\$ – Keith Randall May 5 '14 at 15:27
1
\$\begingroup\$

GolfScript, 80 (77) bytes

~.,~)3%:P[0]*+[4]3*\+256base 64base{'+/''A[a{:0'{,^}/=}/{;}P*'='P*]4>76/"\r
":n*

The above will fit exactly 76 characters in a line, except for the last line. All lines are terminated by CRLF.

Note that RFC 2045 specifies a variable, maximum line length of 76 characters, so at the cost of pretty output, we can save 3 additional bytes.

~.,~)3%:P[0]*+[4]3*\+256base 64base{'+/''A[a{:0'{,^}/=}/{;}P*'='P*]4>{13]n+}/

The above will print one character per line, except for the last line, which can contain 0, 1 or 2 = chars. GolfScript will also append a final LF, which, according to RFC 2045, must be ignored by decoding software.

Example

$ echo '[99 97 102 195 169]' | golfscript base64.gs | cat -A
Y2Fmw6k=^M$
$ echo [ {0..142} ] | golfscript base64.gs | cat -A
AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4^M$
OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx^M$
cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY4=^M$
$ echo '[99 97 102 195 169]' | golfscript base64-sneaky.gs | cat -A
Y^M$
2^M$
F^M$
m^M$
w^M$
6^M$
k^M$
=^M$
$

How it works

~          # Interpret the input string.
.,~)3%:P   # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
[0]*+      # Append that many zero bytes to the input array.
[4]3*\+    # Prepend 3 bytes to the input array to avoid issues with leading zeros.
256base    # Convert the input array into an integer.
64base     # Convert that integer to base 64.
{          # For each digit:
  '+/'     # Push '+/'.
  'A[a{:0' # Push 'A[a{:0'.
  {        # For each byte in 'A[a{:0':
    ,      # Push the array of all bytes up to that byte.
    ^      # Take the symmetric difference with the array below it.
  }/       # Result: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  =        # Retrieve the character corresponding to the digit.
}/         #
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]          # Collect all bytes on the stack into an array.
4>         # Remove the first four, which correspond to the 3 prepended bytes.
76/        # Collect all bytes on the stack into an array and split into 76-byte chunks.
"\r\n":n*  # Join the chunks with separator CRLF and save CRLF as the new line terminator.
\$\endgroup\$
1
\$\begingroup\$

PHP, 200 bytes

<?foreach($g=$_GET as$k=>$v)$b[$k/3^0]+=256**(2-$k%3)*$v;for(;$i<62;)$s.=chr($i%26+[65,97,48][$i++/26]);foreach($b as$k=>$v)for($i=4;$i--;$p++)$r.=("$s+/=")[count($g)*4/3<$p?64:($v/64**$i)%64];echo$r;

Try it online!

You could replace the string ("$s+/=") with an array array_merge(range(A,Z),range(a,z),range(0,9),["+","/","="])

Only to compare which byte count can reach with an not allowed built-in

PHP, 45 bytes

<?=base64_encode(join(array_map(chr,$_GET)));

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 220B

f=a=>{for(s=a.map(e=>('0000000'+e.toString(2)).slice(-8)).join(p='');s.length%6;p+='=')s+='00';return s.match(/.{6}/g).map(e=>'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'[parseInt(e,2)]).join('')+p}

If your browser does not support ES6, you can try with this version (262B) :

function f(a){for(s=a.map(function(e){return ('0000000'+e.toString(2)).slice(-8)}).join(p='');s.length%6;p+='=')s+='00';return s.match(/.{6}/g).map(function(e){return 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'[parseInt(e,2)]}).join('')+p}

f([99, 97, 102, 195, 169]) returns "Y2Fmw6k=".

\$\endgroup\$
  • \$\begingroup\$ Where's the code to split it into 76-char chunks joined with \r\n? \$\endgroup\$ – Peter Taylor May 5 '14 at 15:33
0
\$\begingroup\$

Python - 310, 333

def e(b):
  l=len;c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";r=p="";d=l(b)%3
  if d>0:d=abs(d-3);p+="="*d;b+=[0]*d
  for i in range(0,l(b)-1,3):
    if l(r)%76==0:r+="\r\n"
    n=(b[i]<<16)+(b[i+1]<<8)+b[i+2];x=(n>>18)&63,(n>>12)&63,(n>>6)&63,n&63;r+=c[x[0]]+c[x[1]]+c[x[2]]+c[x[3]]
  return r[:l(r)-l(p)]+p

Somewhat ungolfed:

def e( b ):
    c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    r = p = ""
    d = len( b ) % 3

    if d > 0:
        d = abs( d - 3 )
        p = "=" * d
        b + = [0] * d

    for i in range( 0, len( b ) - 1, 3 ):
        if len( r ) % 76 == 0:
            r += "\r\n"

        n = ( b[i] << 16 ) + ( b[i + 1] << 8 ) + b[i + 2]
        x = ( n >> 18 ) & 63, ( n >> 12 ) & 63, ( n >> 6) & 63, n & 63
        r += c[x[0]] + c[x[1]] + c[x[2]] + c[x[3]]

    return r[:len( r ) - len( p )] + p

Example:

Python's built-in base64 module is only used in this example to ensure the e function has the correct output, the e function itself isn't using it.

from base64 import encodestring as enc

test = [ 99, 97, 102, 195, 169 ]
str  = "".join( chr( x ) for x in test )

control = enc( str ).strip()
output = e( test )

print output            # => Y2Fmw6k=
print control == output # => True
\$\endgroup\$
  • \$\begingroup\$ The question has been clarified to require RFC 2045, so you need to add a bit of code to split the output into 76-char chunks and join with \r\n. \$\endgroup\$ – Peter Taylor May 5 '14 at 9:38
  • \$\begingroup\$ @PeterTaylor fixed. \$\endgroup\$ – Tony Ellis May 5 '14 at 18:21
0
\$\begingroup\$

Jelly, 38 bytes

s3z0Zµḅ⁹b64‘ịØb)FṖ³LN%3¤¡s4z”=Z;€“ƽ‘Ọ

Try it online!

As (almost) every other answer covers the RFC2045 requirement of "at most 76 chars per line with line ending \r\n", I followed it.

How it works

s3z0Zµḅ⁹b64‘ịØb)FṖ³LN%3¤¡s4z”=Z;€“ƽ‘Ọ    Monadic main link. Input: list of bytes

s3z0Z    Slice into 3-item chunks, transpose with 0 padding, transpose back
         Equivalent to "pad to length 3n, then slice into chunks"

µḅ⁹b64‘ịØb)    Convert each chunk to base64
 ḅ⁹b64         Convert base 256 to integer, then to base 64
      ‘ịØb     Increment (Jelly is 1-based) and index into base64 digits

FṖ³LN%3¤¡s4z”=Z    Add correct "=" padding
F                  Flatten the list of strings to single string
 Ṗ      ¡          Repeat "remove last" n times, where
  ³LN%3¤             n = (- input length) % 3
         s4z”=Z    Pad "=" to length 4n, then slice into 4-item chunks

;€“ƽ‘Ọ    Add "\r\n" line separator
;€         Append to each line:
  “ƽ‘       Codepage-encoded list [13,10]
      Ọ    Apply `chr` to numbers; effectively add "\r\n"
\$\endgroup\$
  • \$\begingroup\$ Base decompression can be used here, but ṃØbṙ1¤ is a bit too long for a simple operation. \$\endgroup\$ – user202729 Nov 11 '18 at 4:14
  • \$\begingroup\$ It may be worth asking Dennis to make rotated-base-decompression atom. \$\endgroup\$ – user202729 Nov 11 '18 at 4:18
  • \$\begingroup\$ Fails for 0,0,0. \$\endgroup\$ – user202729 Nov 11 '18 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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