Skip to content

Instantly share code, notes, and snippets.

@zakuroishikuro
Last active August 29, 2015 13:57
Show Gist options
  • Save zakuroishikuro/9562436 to your computer and use it in GitHub Desktop.
Save zakuroishikuro/9562436 to your computer and use it in GitHub Desktop.
36進数で英文を書いてみる
# 36進数でちょっと遊ぶ (簡単な英文を書く)
# http://qiita.com/manuluu/items/ad2c471a1a1c6fd91a63
module Base36Utils
def get_symbols(str)
syms = str.scan(/[^a-zA-Z]/).uniq.sort.join
end
def to_base36(base10, symbols="")
base10.to_s(36).gsub(/\d/){|d|symbols[d.to_i]}
end
def to_base10(base36, symbols="")
base36.gsub(/[#{symbols}]/){|c|symbols.index(c)}.to_i(36)
end
end
require_relative 'base36utils'
include Base36Utils
str = "I thought what I'd do was, I'd pretend I was one of those deaf-mutes"
syms = get_symbols(str)
base10 = to_base10(str, syms)
base36 = to_base36(base10, syms)
print "original: "; p str
#=> "I thought what I'd do was, I'd pretend I was one of those deaf-mutes"
print "symbols: "; p syms
#=> " ',-"
puts
puts "encoded (base10): "; p base10
#=> 3373562398143598293391832131714159190003724948263861387893016685268379127404427353734077228848031196783204
puts "decoded (base36): "; p base36
#=> "i thought what i'd do was, i'd pretend i was one of those deaf-mutes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment