1# add as many different characters as you want
2alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
3
4
5def convert(num, base=2):
6 assert base <= len(alpha), f"Unable to convert to base {base}"
7 converted = ""
8 while num > 0:
9 converted += alpha[num % base]
10 num //= base
11
12 if len(converted) == 0:
13 return "0"
14 return converted[::-1]
15
16
17print(convert(15, 8)) # 17
1chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" #these are the digits you will use for conversion back and forth
2charsLen = len(chars)
3
4def numberToStr(num):
5 s = ""
6 while num:
7 s = chars[num % charsLen] + s
8 num //= charsLen
9
10 return(s)
11
12def strToNumber(numStr):
13 num = 0
14 for i, c in enumerate(reversed(numStr)):
15 num += chars.index(c) * (charsLen ** i)
16
17 return(num)