python covert base 10 to base 2 code example

Example 1: convert between bases python

chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" #these are the digits you will use for conversion back and forth
charsLen = len(chars)

def numberToStr(num):
  s = ""
  while num:
    s = chars[num % charsLen] + s
    num //= charsLen

  return(s)

def strToNumber(numStr):
  num = 0
  for i, c in enumerate(reversed(numStr)):
    num += chars.index(c) * (charsLen ** i)

  return(num)

Example 2: how to convert from base 2 to base 10 in python

def getBaseTen(binaryVal):
	count = 0

	#reverse the string
    binaryVal = binaryVal[::-1]

    #go through the list and get the value of all 1's
	for i in range(0, len(binaryVal)):
    	if(binaryVal[i] == "1"):
            count += 2**i
    
    return count