Created
July 18, 2012 03:49
-
-
Save armstrjare/3134039 to your computer and use it in GitHub Desktop.
This file contains 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
MAP = [] | |
('A'...'Z').each do |c| | |
MAP << c | |
end | |
('a'..'z').each do |c| | |
MAP << c | |
end | |
('0'..'9').each do |c| | |
MAP << c.to_s | |
end | |
BASE = MAP.length | |
def encode(number) | |
res = [] | |
while number > 0 | |
rem = number % BASE | |
res << MAP[rem] | |
number = (number -rem)/BASE | |
end | |
res.join | |
end | |
def decode(str) | |
number = 0 | |
factor = 1 | |
str.split(//).each do |d| | |
number += factor*MAP.index(d) | |
factor *= BASE | |
end | |
number | |
end | |
# | |
# Time.now.to_i | |
# => 1342583287 | |
# encode(Time.now.to_i) | |
# => "Ff77kB" | |
# | |
# decode("Ff77kB") | |
# => 1342583287 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment