Skip to content

Instantly share code, notes, and snippets.

@dux
Created October 11, 2011 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dux/1278292 to your computer and use it in GitHub Desktop.
Save dux/1278292 to your computer and use it in GitHub Desktop.
Base62 encode and decode for numbers
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