fork download
  1. TABLE = [*?A..?Z, *?a..?z, *?0..?9, ?+, ?/]
  2.  
  3. def decode str
  4. str.delete(?=).each_char.map{|c| '%06b'%TABLE.index(c)}.join.scan(/.{8}/).map{|e| e.to_i(2).chr}.join
  5. end
  6.  
  7. def encode str
  8. str.codepoints.flat_map{|e| ('%08b'%e).chars}.each_slice(6).map(&:join).tap{|a| a[-1] = a[-1].ljust(6, ?0)}.map{|e| TABLE[e.to_i(2)]}.join.tap{|s| s.concat(?=*(-s.size%4))}
  9. end
  10.  
  11. puts"input:\t#{$_.chomp}\nencode:\t#{e = encode$_}\ndecode:\t#{decode(e)}"while gets
  12.  
Success #stdin #stdout 0s 28688KB
stdin
ABCDEFG
Hello, World!
0123456789"#$%&'()`=@
stdout
input:	ABCDEFG
encode:	QUJDREVGRwo=
decode:	ABCDEFG
input:	Hello, World!
encode:	SGVsbG8sIFdvcmxkIQo=
decode:	Hello, World!
input:	0123456789"#$%&'()`=@
encode:	MDEyMzQ1Njc4OSIjJCUmJygpYD1A
decode:	0123456789"#$%&'()`=@