base62.py 822 Bytes
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
class Base62() :
set = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

def encode (self,i):

if (i <= 0) :
return "0"
ret = ""
while (i > 0) :
ret = self.set[i%62] + ret
i = i / 62
return ret

def decode (self,s):

if (str(s) <= 0) :
return 0
ret = 0
length = len(s)
for i in range(length):
c = s[i]
for j in range(62) :
if c == self.set[j] :
ret = ret + (j * pow(62,(length - i - 1)))
break
return ret