如何使用自定义字母将 Base 10 数字转换为 Base64 数字?

How to convert a Base10 number to a Base64 number with custom alphabet?

我正在编写一个程序,该程序需要能够将 base10 数字转换为 base64 数字,并使用此字母表再次返回:

"0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM. "

我查看了其他堆栈溢出问题,但是 none 这些解决方案有效。如果有任何帮助,我将不胜感激。

试试这个

s = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM. "
def encode(n):
  ans = ''
  if n == 0:
    ans = s[0]
  else:
    while n:
      r, n = n % 64, n // 64
      ans += s[r]
  return ans[::-1]

def decode(n):
  ans, m = 0, 1
  for char in n[::-1]:
    ans += s.index(char) * m
    m *= 64
  return ans

print(encode(987654321))
print(decode('VZEnF'))

输出:

VZEnF
987654321