Question

I've tried to create a simple method to convert a string into a base-10 integer (in Python):

def strToNum(strData, num=0 ,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
    return ((len(strData)==0) and num) or (strToNum(strData[0:-1], num+numerals.index(strData[-1])**len(strData)))

It doesn't seem to work. When I tested out 'test' as the string it outputted: 729458. And when I used some online tools to convert, I got: 1372205.

Was it helpful?

Solution

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

OTHER TIPS

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

Try this:

def convert(string: str) -> int:
    for base in range(0, 36):
        try:
            if str(int(string, base)) == string:
                return int(string, base)
                break
        except ValueError:
            pass
        finally:
            pass
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top