Converting binary to decimal integer output

前端 未结 9 2339
暗喜
暗喜 2021-02-13 16:58

I need to convert a binary input into a decimal integer. I know how to go from a decimal to a binary:

n = int(raw_input(\'enter a number: \'))
print \'{0:b}\'.fo         


        
9条回答
  •  后悔当初
    2021-02-13 17:42

    a = input('Enter a binary number : ')
    ar = [int(i) for  i in a]
    ar  = ar[::-1]
    res = []
    for i in range(len(ar)):
        res.append(ar[i]*(2**i))
    sum_res = sum(res)      
    print('Decimal Number is : ',sum_res)
    

提交回复
热议问题