Skip to main content
3 of 9
deleted 2 characters in body
skibrianski
  • 1.3k
  • 7
  • 10

perl, 139 bytes

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

ungolfed:

my @x = ('A'..'Z','a'..'z',0..9,'+','/');
my $in = join '', <>;
my $bits = unpack 'B*', $in;
my @six_bit_groups = split /(.{6})/, $bits;
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;
}
skibrianski
  • 1.3k
  • 7
  • 10