Skip to content

Instantly share code, notes, and snippets.

@geemus geemus/base.rb

Created Feb 24, 2009
Embed
What would you like to do?
module Base
module_function
BASE_CHARACTERS = '0123456789BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz'
def encode(int)
str = ''
while int > 0
character = int % BASE_CHARACTERS.length
int = int / BASE_CHARACTERS.length
str = BASE_CHARACTERS[character..character] + str
end
str
end
def decode(str)
int = 0
str.reverse.split('').each_with_index do |char, i|
int += BASE_CHARACTERS.index(char) * (BASE_CHARACTERS.length ** i)
end
int
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.