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
If you want/need to do it without int
:
sum(int(c) * (2 ** i) for i, c in enumerate(s[::-1]))
This reverses the string (s[::-1]
), gets each character c
and its index i
(for i, c in enumerate(
), multiplies the integer of the character (int(c)
) by two to the power of the index (2 ** i
) then adds them all together (sum()
).