convert between bases python

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

showing results for - "convert between bases python"
Carla
09 Apr 2019
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
Mathilda
24 Feb 2017
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)
queries leading to this page
convert to any base pythonhow to convert base 10 to base 2 in pythonbase convert pythonalphanumeric to int pythonbase conversions in pythomconvert to base 36 pythonconvert bases in pythonextract number from alphanumeric string pythonhow to convert base pythonhow to convert base in pythonbase n coding in pythonconvert alphanumeric to ascii numeric pythonjconvert alphanumeric string to integer in python8 to base 2 notation pythonchange alphanumeric to numeric in pandasconvert alphanumeric to ascii numeric pythonprogram to convert number between bases pythonconvert any base to any base in pythonhow to change base system in pythonpython convert basehow to change number base in pythonbase converter pythonconvert bases pythonbase conversion in pythonbase 10 to base 2 python converterbase 20 to base 2 in pythonwrite a program that converts decimal integers into a given base pythonconvert text to a different base in pythonpython base x number to decimal conversionconvert a number to nay base in pythonhow to convert bases in pythonpython convert to base 62change base pyhton base conversion pythonconvert alphanumeric to int in pythonbase converter python codeconvert string to alphanumeric pythonconverting base 10 to base 2 pythonconvert from base 10 to base 20 pythonalphanumeric to numeric pythonhow to store alphanumeric as int pythonconverting alphanumeric to numeric in pandaspython convert alphanumeric to numericget only numeric value from string pythonhow to do base conversion in pythonchaging alphanumeric to numeric in pandaspython convert baseschange a base 10 to a base 2 pythonconvert to base pythonpython base conversionpython base 10 to base 16convert base pythonconvert alphanumeric to string pythonconvert alphanumeric to numericconverting decimal to base 7 using stack pythonhow to change the base of a number in pythonpython to basepython base converterfrom python basehow to convert between bases in pythonconvert between bases pythonbase convertor code for pythonconvert number in base 7 using pythonconvert alphanumeric to numeric in pythonbase 10 to base 16 pythonhow to convert alphanumeric to numeric in pythonalphanumeric to numeric djangoalphanumeric to int conversion pythonturn alphanumeric into numeric value pythonsimple python program to convert decimal to base 16 representationsetting base for binary pythonpython convert to base 16how to convert alphanumeric string to int in pythonconvert base in pythonpython program convert a base 4 number to base 16 directlypython convert to base 10how to do base in pythonconvert base 10 to base 2 pythonpython change to base 2python base converter codeconvert a decimal number to a number of a given base b pythonconvert alphanumeric to numeric pythonconvert alphanum to string in pythonconvert between bases python