Convert string to decimal (base 10) in Python

Solution 1:

You can simply use int here:

>>> strs = 'test'
>>> int(strs, 36)
1372205

Or define your own function:

def func(strs):
    numerals = "0123456789abcdefghijklmnopqrstuvwxyz"
    return sum(numerals.index(x)*36**i for i, x in enumerate(strs[::-1]))
... 
>>> func(strs)
1372205

Solution 2:

If your input is in UTF-8 you can encode each byte to Base10, rather than limit yourself to some fixed set of numerals. The challenge then becomes decoding. Some web-based Base10 encoders separate each encoded character/byte with a space. I opted to left-pad with a null character which can be trimmed out.

I am sure there is plenty of room for optimisation here, but these two functions fit my needs:

Encode:

def base10Encode(inputString):
    stringAsBytes = bytes(inputString, "utf-8")
    stringAsBase10 = ""
    for byte in stringAsBytes:
        byteStr = str(byte).rjust(3, '\0') # Pad left with null to aide decoding
        stringAsBase10 += byteStr
    return stringAsBase10

Decode:

def base10Decode(inputString):
    base10Blocks = []
    for i in range(0, len(inputString), 3):
        base10Blocks.append(inputString[i:i+3])
    decodedBytes = bytearray(len(base10Blocks))
    for i, block in enumerate(base10Blocks):
        blockStr = block.replace('\0', '')
        decodedBytes[i] = int(blockStr)
    return decodedBytes.decode("utf-8")