Skip to content

Instantly share code, notes, and snippets.

@ssut
Created March 14, 2014 19:37
Show Gist options
  • Save ssut/9555137 to your computer and use it in GitHub Desktop.
Save ssut/9555137 to your computer and use it in GitHub Desktop.
Base64 impl written Ruby.
class String
def map
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
end
def encode64
self.bytes.to_a.map {|a|a.ord.to_s(2).rjust(8, '0')}.join.scan(/.{6}/).map {|a|map[a.to_i(2)]}.join
end
def decode64
self.split('').map {|a|map.index(a)}.map {|a|a.to_s(2).rjust(6,'0')}.join.scan(/.{8}/).map{|a|a.to_i(2).chr}.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment