Base Converter

Converts an integer to a string representation in a given base (from 2 to 36).

Contributed by @witttccchhher

python
def convert(num: int, targetBase: int) -> str:
    assert targetBase <= 36
 
    result = ""
    while num > 0:
        num, reminder = divmod(num, targetBase)
        if reminder > 9:
            reminder = chr(ord("A") + reminder - 10)
        result = f"{reminder}{result}"
 
    return result
python
convert(101, 2) # 1100101
convert(42, 64) # Error, the base should be from 2 to 36
GitHubEdit on GitHub