11 月 062020
 
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time       : 2020/11/3 14:59
# @Author     : Harvey Mei <harvey.mei@msn.com>
# @FileName   : base36.py
# @IDE        : PyCharm
# @GitHub     :https://github.com/harveymei/

import string


# base10 to base36
def encode(number):
    alphabet = string.digits + string.ascii_lowercase
    value = ''

    while number != 0:
        number, index = divmod(number, len(alphabet))
        value = alphabet[index] + value

    return value or '0'


# base36 to base10
def decode(value):
    return int(value, 36)


# 循环,当number > 0时求商取余,将余数作为字符串切片取值
print(divmod(1024, 36))
print((string.digits + string.ascii_lowercase)[16])
# 'g' + ''

print(divmod(28, 36))
print((string.digits + string.ascii_lowercase)[28])
# 's' + 'g'

# 当number == 0时退出循环
print(divmod(0, 36))
print((string.digits + string.ascii_lowercase)[0])

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)