Skip to content

Instantly share code, notes, and snippets.

@atomaka
Created September 24, 2012 02:48
Show Gist options
  • Save atomaka/3773932 to your computer and use it in GitHub Desktop.
Save atomaka/3773932 to your computer and use it in GitHub Desktop.
BASE52_CHARACTERS = Array('0'..'9') + Array('A'..'Z') + Array('a'..'z') - 'AEIOUaeiou'.split(//)
class String
def from_52
i = 0
decoded = 0
self.split(//).reverse.each do |a|
place = 52 ** i
decoded += BASE52_CHARACTERS.index(a) * place
i += 1
end
decoded
end
end
class Integer
def to_52
input = self
encoded = ''
while input > 0
encoded = BASE52_CHARACTERS[input % 52].to_s + encoded
input /= 52
end
encoded
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment