【问题标题】:How do I convert a number to base 64 in python?如何在python中将数字转换为base 64?
【发布时间】:2019-12-02 17:30:21
【问题描述】:

所以我正在尝试制作这个以 10 为基数到 64 基数的转换器,对我来说一切都很好。

# digits that will represent the base 64 number
base64Digits = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','-','!']

def base10to64converter(num):

    # Define the number of digits the result will have (limit of 2k)
    i = 1
    while i<2000:
        if num<64**i:
            digitNum = i
            i+=2000
        i+=1

    digits = []

    # calculate the number
    while digitNum > 0:
        digits.append(num // (64**(digitNum-1)))
        num %= (64**(digitNum-1))
        digitNum-=1

    result = ''
    j = 0

    # transform the digits stored in the array into an string
    while j < len(digits):
        result += base64Digits[digits[j]]
        j+=1

    return result

但我想使用大数字,所以我尝试使用更大的数字。我从尝试 10^2000 开始。有效。没什么奇怪的,但后来我尝试了 10^2000-1,但由于某种原因它不起作用。我得到一个索引错误。 我调试了一下,发现在数字数组的第 750 位附近,有一个值为 64 的数字。这些数字不应该超过 63,这就是为什么没有base64Digits[64]。 这很奇怪,因为如果数字的值为 64,那么这意味着前面的数字应该在其值中定义为 +1,但我不知道是什么导致了这个问题,有人可以帮我吗?

【问题讨论】:

  • 这能回答你的问题吗? Base 62 conversion
  • @Fourier:可能不是,因为问题本质上是“我的代码有什么问题?”,而不是“我该如何解决这个问题?”!
  • 我正在运行base10to64converter(10**200-1),我没有收到任何索引错误。
  • @goodvibration 对不起,我的错字,我的意思是 10**2000-1
  • 我在 10**2000-1 上也没有出错。

标签: python python-3.x


【解决方案1】:

你的代码没有出错,但验证它是否正常工作的一个好方法是用几种不同的方式实现算法并进行比较:

# digits that will represent the base 64 number
base64Digits = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','-','!']

def base10to64converter(num):

    # Define the number of digits the result will have (limit of 2k)
    i = 1
    while i<2000:
        if num<64**i:
            digitNum = i
            i+=2000
        i+=1

    digits = []

    # calculate the number
    while digitNum > 0:
        digits.append(num // (64**(digitNum-1)))
        num %= (64**(digitNum-1))
        digitNum-=1

    result = ''
    j = 0

    # transform the digits stored in the array into an string
    while j < len(digits):
        result += base64Digits[digits[j]]
        j+=1

    return result

def alternate(n):
    if n < 0:
        raise ValueError('negative numbers not supported')
    base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!'
    digits = []
    while n != 0:
        n,r = divmod(n,64)
        digits.append(base[r])
    if not digits:
        return '0'
    return ''.join(reversed(digits))

trials = [0,1,64,10**2000-1,10**2000]
for trial in trials:
    n = base10to64converter(trial)
    print(n)
    assert n == alternate(trial)

你的方法和我的方法在五次试验中返回相同的答案。

【讨论】:

  • 实际上,我今天只是尝试运行我的代码而不进行任何编辑,我实际上不明白为什么它上次没有工作但感谢您的建议:)
猜你喜欢
  • 2015-08-09
  • 1970-01-01
  • 2021-09-19
  • 2018-03-26
  • 2014-02-26
  • 2018-01-22
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
相关资源
最近更新 更多