Skip to content

Instantly share code, notes, and snippets.

@kenichi
Created June 14, 2012 18:28
Show Gist options
  • Save kenichi/2931996 to your computer and use it in GitHub Desktop.
Save kenichi/2931996 to your computer and use it in GitHub Desktop.
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