Skip to content

Instantly share code, notes, and snippets.

@armstrjare
Created July 18, 2012 03:49
Show Gist options
  • Save armstrjare/3134039 to your computer and use it in GitHub Desktop.
Save armstrjare/3134039 to your computer and use it in GitHub Desktop.
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