base60 (en|de)coding - see http://tantek.pbworks.com/w/page/19402946/NewBase60
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def base60_decode | |
n = 0 | |
self.each_byte do |c| | |
case | |
when c >= 48 && c <= 57 | |
c -= 48 | |
when c >= 65 && c <= 72 | |
c -= 55 | |
when c == 73 || c == 108 | |
c = 1 | |
when c >= 74 && c <= 78 | |
c -= 56 | |
when c == 79 | |
c = 0 | |
when c >= 80 && c <= 90 | |
c -= 57 | |
when c == 95 | |
c = 34 | |
when c >= 97 && c <= 107 | |
c -= 62 | |
when c >= 109 && c <= 122 | |
c -= 63 | |
else | |
c = 0 | |
end | |
n = (60 * n) + c | |
end | |
n | |
end | |
end | |
class Integer | |
BASE60_MAP = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz" | |
def base60_encode | |
return self if self == 0 | |
s = "" | |
n = self | |
while n > 0 do | |
d = n % 60; | |
s = BASE60_MAP[d] + s; | |
n = (n-d)/60; | |
end | |
s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment