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

perl, 126 bytes

reads stdin, outputs to stdout

print map{$l=length()/2%3;[A..Z,a..z,0..9,"+","/"]b".substr$_.0 x4,0,6],$l?"="x(3-$l):""if$_}unpack("B*",join"",<>)=~/.{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;
}
skibrianski
  • 1.3k
  • 7
  • 10