Convert binary to decimal integer output

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

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

I need to go in the opposite direction. My professor said that when he checks our code, he is going to enter 11001, and he should get 25back. I looked through our notes and I cannot figure out how to do this. Google and other online resources also did not help.

The biggest problem is that we are not allowed to use the built-in functions. I understand why we are not allowed to use them, but this complicates this problem, since I know that Python has a built-in function for binary to decimal.

+4
source share
7 answers

You can use intand set the base to 2(for binary):

>>> binary = raw_input('enter a number: ')
enter a number: 11001
>>> int(binary, 2)
25
>>>

However, if you cannot use intlike this, you can always do this:

binary = raw_input('enter a number: ')
decimal = 0
for digit in binary:
    decimal = decimal*2 + int(digit)
print decimal

Below is a demo:

>>> binary = raw_input('enter a number: ')
enter a number: 11001
>>> decimal = 0
>>> for digit in binary:
...     decimal = decimal*2 + int(digit)
...
>>> print decimal
25
>>>
+16
source

If you need / need to do this without int:

sum(int(c) * (2 ** i) for i, c in enumerate(s[::-1]))

This changes the string ( s[::-1]), gets each character cand its index i( for i, c in enumerate(), multiplies the integer of the character ( int(c)) by two by the power of the index ( 2 ** i), then adds them all together ( sum()).

+2
source
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)
+1

-, . , ! , , . , , , :

def __degree(number):
    power = 1

    while number % (10**power) != number:
        power += 1

    return power

def __getDigits(number):
    digits = []
    degree = __degree(number)

    for x in range(0, degree):
        digits.append(int(((number % (10**(degree-x))) - (number % (10**(degree-x-1)))) / (10**(degree-x-1))))
    return digits

def binaryToDecimal(number):
    list = __getDigits(number)
    decimalValue = 0
    for x in range(0, len(list)):
        if (list[x] is 1):
            decimalValue += 2**(len(list) - x - 1)
    return decimalValue

, Python , , . , , , - , , . , , , , , , ! , ! , :

binaryNum = int(input("Enter a binary number: "))

print(binaryToDecimal(binaryNum))

This outputs the correct result. Hurrah!

0
source

The input can be string or integer.

num = 1000  #or num = '1000'  
sum(map(lambda x: x[1]*(2**x[0]), enumerate(map(int, str(num))[::-1])))

# 8
0
source

This is the complete thing.

binary = input('enter a number: ')
decimal = 0
for digit in binary:
decimal= decimal*2 + int(digit)

print (decimal)
0
source

Try this solution:

def binary_int_to_decimal(binary):
n = 0
for d in binary:
    n = n * 2 + d

return n
0
source

Source: https://habr.com/ru/post/1526821/


All Articles