参考资料:https://www.cnblogs.com/qiernonstop/p/3634354.html
分析思路:
实际代码:
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def base62_encode(num, alphabet=ALPHABET):
"""Encode a number in Base X
`num`: The number to encode
`alphabet`: The alphabet to use for encoding
"""
if (num == 0):
return alphabet[0]
arr = []
base = len(alphabet)
while num:
rem = num % base
num = num // base
arr.append(alphabet[rem])
arr.reverse()
return ''.join(arr)
def base62_decode(string, alphabet=ALPHABET):
"""Decode a Base X encoded string into the number
Arguments:
- `string`: The encoded string
- `alphabet`: The alphabet to use for encoding
"""
base = len(alphabet)
strlen = len(string)
num = 0
idx = 0
for char in string:
power = (strlen - (idx + 1))
num += alphabet.index(char) * (base ** power)
# print('n:', power, num)
idx += 1
return num
def mid_to_url(midint):
"""
>>> mid_to_url(3491700092079471)
'yCtxn8IXR'
>>> mid_to_url(3486913690606804)
'yAt1n2xRa'
"""
midint = str(midint)[::-1]
size = len(midint) / 7 if len(midint) % 7 == 0 else len(midint) / 7 + 1
result = []
for i in range(size):
s = midint[i * 7: (i + 1) * 7][::-1]
s = base62_encode(int(s))
s_len = len(s)
if i < size - 1 and len(s) < 4:
s = '0' * (4 - s_len) + s
result.append(s)
result.reverse()
return ''.join(result)
def url_to_mid(url):
"""
>>> url_to_mid('z0JH2lOMb')
3501756485200075L
>>> url_to_mid('z0Ijpwgk7')
3501703397689247L
"""
url = str(url)[::-1]
size = len(url) / 4 if len(url) % 4 == 0 else len(url) // 4 + 1
result = []
for i in range(size):
s = url[i * 4: (i + 1) * 4][::-1]
s = str(base62_decode(str(s)))
s_len = len(s)
if i < size - 1 and s_len < 7:
s = (7 - s_len) * '0' + s
result.append(s)
result.reverse()
return int(''.join(result))
if __name__ == '__main__':
# print(url_to_mid('HBhZV1O3m')) # 4372726779455825
# print(base62_decode('DFTj')) # 9455825
print(base62_encode(4388873230430736)) # HBhZV1O3m
---- END---