0

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?

3LL KNJ
  • 3
  • 2

1 Answers1

0

I don't get a failure with your code, but a good way to verify it is working properly is implement the algorithm a couple different ways and compare:

# 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)

Both your method and mine return the same answer on the five trials.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Actually, I just tried to run my code today without editing anything, i don't actually get why it didn't work last time but thanks for the suggestion :) – 3LL KNJ Dec 03 '19 at 12:44