So I'm trying to make this base 10 to base 64 converter and it all looks fine for me.
# 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
But I want to work with big numbers so i tried using bigger numbers. I started by trying 10^2000. It worked. Nothing strange, but then I tried 10^2000-1, and for some reason it didn't work. I got an index error.
I debugged a little and found out that around the 750th digit in the digits array, there was a digit with value 64. The digits aren't supposed to go further than 63, that's why there is no base64Digits[64]
.
That is strange because if the digit has a value of 64, then it means that the previus digit should have been defined with +1 in its value, but i can't figure out what is causing this problem, can someone help me?