Converting decimal to base58check using Python 3.7

222 views Asked by At

As part of the routine I have used

ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

def convertToBase58(num):
    sb = ''

    while (num > 0):
        r = num % 58   # divide by 58 and gives the remainder
        sb = sb + ALPHABET[r]
        num = num / 58;
    return sb[::-1]

This comes back with an error saying that r has to be an integer. But using the % operator seems to define r as integer. What have I missed, please ?

1

There are 1 answers

0
Steve On

The r variable has not been defined as an integer. However, the num variable as been defined as a float on the second iteration of the loop. It's a result of the last line in the loop num = num / 58.

The division operator returns a float. In Python 3.5+ use // to return an integer.

Older versions of Python can use math.floor