【发布时间】: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