Base 62 转换 [英] Base 62 conversion
问题描述
如何将整数转换为基数 62(如十六进制,但使用以下数字:'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ').
我一直试图为它找到一个好的 Python 库,但它们似乎都忙于转换字符串.Python base64 模块只接受字符串并将单个数字转换为四个字符.我正在寻找类似于 URL 缩短器使用的东西.
没有用于此的标准模块,但我已经编写了自己的函数来实现.
BASE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"定义编码(数字,字母):""" 将一个正数编码为 Base X 并返回字符串.参数:- `num`:要编码的数字- `alphabet`:用于编码的字母表"如果数量 == 0:返回字母表[0]arr = []arr_append = arr.append # 提取绑定方法以加快访问速度._divmod = divmod # 访问本地人更快.基数 = len(字母)编号:num, rem = _divmod(num, base)arr_append(字母[rem])arr.reverse()返回 ''.join(arr)定义解码(字符串,字母表=BASE62):"""将一个Base X编码的字符串解码成数字参数:- `string`:编码后的字符串- `alphabet`:用于解码的字母表"基数 = len(字母)strlen = len(字符串)数量 = 0idx = 0对于字符串中的字符:功率 = (strlen - (idx + 1))num +=alphabet.index(char) * (base ** power)idx += 1返回编号
请注意,您可以为其提供任何字母表以用于编码和解码.如果您不使用 alphabet
参数,您将获得在第一行代码中定义的 62 个字符的字母表,因此编码/解码为 62 基数.
希望这会有所帮助.
PS - 对于 URL 缩短器,我发现最好省略一些令人困惑的字符,例如 0Ol1oI 等.因此我使用这个字母表来满足我的 URL 缩短需求 - "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
>
玩得开心.
How would you convert an integer to base 62 (like hexadecimal, but with these digits: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ').
I have been trying to find a good Python library for it, but they all seems to be occupied with converting strings. The Python base64 module only accepts strings and turns a single digit into four characters. I was looking for something akin to what URL shorteners use.
There is no standard module for this, but I have written my own functions to achieve that.
BASE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def encode(num, alphabet):
"""Encode a positive number into Base X and return the string.
Arguments:
- `num`: The number to encode
- `alphabet`: The alphabet to use for encoding
"""
if num == 0:
return alphabet[0]
arr = []
arr_append = arr.append # Extract bound-method for faster access.
_divmod = divmod # Access to locals is faster.
base = len(alphabet)
while num:
num, rem = _divmod(num, base)
arr_append(alphabet[rem])
arr.reverse()
return ''.join(arr)
def decode(string, alphabet=BASE62):
"""Decode a Base X encoded string into the number
Arguments:
- `string`: The encoded string
- `alphabet`: The alphabet to use for decoding
"""
base = len(alphabet)
strlen = len(string)
num = 0
idx = 0
for char in string:
power = (strlen - (idx + 1))
num += alphabet.index(char) * (base ** power)
idx += 1
return num
Notice the fact that you can give it any alphabet to use for encoding and decoding. If you leave the alphabet
argument out, you are going to get the 62 character alphabet defined on the first line of code, and hence encoding/decoding to/from 62 base.
Hope this helps.
PS - For URL shorteners, I have found that it's better to leave out a few confusing characters like 0Ol1oI etc. Thus I use this alphabet for my URL shortening needs - "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
Have fun.
这篇关于Base 62 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!