bases.py

Created by j-yang19

Created on May 01, 2018

202 Bytes

Conversion between decimal base and other bases

To convert (11010)2 to decimal:

>>> fb(2)
# Enter the digits in reverse order in the input
0
1
0
1
1
 # space to terminate
26

To convert (100)10 to octal:

>>> tb(100, 8)
# tb prints the digits in reverse order:
4
4
1  # = (144)8


def fb(base):
  s=0
  n=0
  while True:
    d=input()
    if d==' ':
      break
    s+=base**n*int(d)
    n+=1
  return s
def tb(num, base):
  while num:
    print(num%base)
    num//=base