Skip to content

Instantly share code, notes, and snippets.

@jrimmer
Created May 6, 2010 06:06
Show Gist options
  • Save jrimmer/391831 to your computer and use it in GitHub Desktop.
Save jrimmer/391831 to your computer and use it in GitHub Desktop.
class B62
CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split('')
BASE = 62
def self.encode(value)
s = []
while value >= BASE
value, rem = value.divmod(BASE)
s << CHARS[rem]
end
s << CHARS[value]
s.reverse.to_s
end
def self.decode(str)
str = str.split('').reverse
total = 0
str.each_with_index do |v,k|
total += (CHARS.index(v) * (BASE ** k))
end
total
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment