Created
October 11, 2011 14:52
-
-
Save dux/1278292 to your computer and use it in GitHub Desktop.
Base62 encode and decode for numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Base62 | |
MAP = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a | |
def encode(numeric) | |
raise TypeError unless numeric.kind_of?(Numeric) | |
return '0' if numeric.zero? | |
s = '' | |
while numeric > 0 | |
s << Base62::MAP[numeric % 62] | |
numeric /= 62 | |
end | |
s.reverse | |
end | |
def decode(base62) | |
s = base62.to_s.reverse.split('') | |
total = 0 | |
s.each_with_index do |char, index| | |
if ord = MAP.index(char) | |
total += ord * (62 ** index) | |
else | |
raise ArgumentError, "#{base62} has #{char} which is not valid" | |
end | |
end | |
total.to_s | |
end | |
module_function :encode, :decode | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment