python convert base

Solutions on MaxInterview for python convert base by the best coders in the world

showing results for - "python convert base"
Dilan
26 Sep 2018
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
Martina
15 Jul 2018
1alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2
3
4def convert(num, base=2):
5    assert base <= len(alpha), f"Unable to convert to base {base}"
6    converted = ""
7    while num > 0:
8        converted += alpha[num % base]
9        num //= base
10
11    if len(converted) == 0:
12        return "0"
13    return converted[::-1]
14  
15
16print(convert(15, 8))  # 17
queries leading to this page
python program convert a base 4 number to base 16 directlypython base 10 to base 16python convert to base 10python change to base 2base conversion pythonhow to convert base 10 to base 2 in pythonhow to do base conversion in pythonconvert base python codepython convert basebase conversion in pythonhow to change the base of a number in pythonprogram to convert number between bases pythonhow to change base system in pythonconvert base pythonbase conversion program pythonhow to change number base in pythonconvert any base to any base pythonconvert to base pythonconvert text to a different base in pythonconvert a decimal number to a number of a given base b pythonconverting decimal to base 7 using stack pythonconvert bases in pythonpytho convert a string to base 16convert any base to any base in pythonchange base pyhton convert to base 10 pythonbase convertor code for pythonbase 10 to base 2 python converterbase converter pythonbase conversion in pyhtonpython convert to base 62python base conversionconvert number in base 7 using pythonconvert bases pythonconversion to base pythonfrom python basepython base converterpython convert base45write a program that converts decimal integers into a given base pythonhow to convert bases in pythonbase convert pythonpython base converter codepython convert to base 16convert to base 36 pythonconvert between bases pythonsimple python program to convert decimal to base 16 representationsetting base for binary pythonpython base x number to decimal conversion8 to base 2 notation pythonbase conversions in pythomhow to convert between bases in pythonconvert a number to nay base in pythondecimal to any base conversion pythonbase to base pythonhow to convert base in pythonconverting base 10 to base 2 pythonchange a base 10 to a base 2 pythonbase converter python codeconvert base in pythonhow to convert base pythonpython convert to any basehow to do base in pythonconvert to any base pythonbase 20 to base 2 in pythonconvert base 10 to base 2 pythonbase 10 to base 16 pythonbase n coding in pythonconvert from base 10 to base 20 pythonpython convert basesany base to any base conversion in pythonpython to basepython convert base