gpt4 book ai didi

Python base 36 编码

转载 作者:IT老高 更新时间:2023-10-28 21:58:47 25 4
gpt4 key购买 nike

如何在 Python 中以 36 为基数对整数进行编码,然后再次解码?

最佳答案

你试过维基百科的示例代码吗?

def base36encode(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
"""Converts an integer to a base36 string."""
if not isinstance(number, (int, long)):
raise TypeError('number must be an integer')

base36 = ''
sign = ''

if number < 0:
sign = '-'
number = -number

if 0 <= number < len(alphabet):
return sign + alphabet[number]

while number != 0:
number, i = divmod(number, len(alphabet))
base36 = alphabet[i] + base36

return sign + base36

def base36decode(number):
return int(number, 36)

print base36encode(1412823931503067241)
print base36decode('AQF8AA0006EH')

关于Python base 36 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1181919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com