you can use this code:
binary= raw_input("Binary: ")
d= int(binary, 2)
print d
Answer from apk on Stack Overflowyou can use this code:
binary= raw_input("Binary: ")
d= int(binary, 2)
print d
To convert binary value to decimal you need to do the following:
Take the least significant bit and multiply it by 2^0, then take the next least significant beat and multiply it by 2^1, next one by 2^2 and so on...
Let's say, for example you need to convert a number 1010 to decimal:
You would have 0*2^0 + 1*2^1 + 0*2^2 + 1*2^3 = 0 + 2 + 0 + 8 = 10
So in your python code, you need to:
- read the int that the user inputted (representing the binary value).
- convert that int and convert it to string, so you can break it into list of digits
- make a list of digits from the string you created (a list int python can be created from a string not an int, that's why you need the conversion to string first)
- go trough that list of bits in reverse and multiply every bit by 2^k, k being the counter starting from 0
Here's the code that demonstrates what I just tried to explain:
user_input = int(input("enter a binary value"))
bits = list(str(user_input))
decimal = 0
counter = 0
for i in reversed(bits):
decimal += 2**counter * int(i)
counter+=1
print 'The decimal value is: ', decimal
For example: [1,0,0,1] should give output 9
Python binary to decimal converter (without using built-ins) - Code Review Stack Exchange
python - how can I convert binary to decimal without using bin method - Stack Overflow
python - Converting binary to decimal integer output - Stack Overflow
Trying to convert Binary to Decimal to Hexadecimal in Python without inbuilt Functions - Stack Overflow
Why use a function to convert binary to decimal in Python?
Is it possible to convert binary to decimal in a single line in Python?
How does Python handle leading zeros in binary input?
To convert from binary to decimal, just do:
int(binary_string, 2) # The 2 is the base argument from which we convert.
Demo:
>>> int('0b10110', 2)
22
Note- There are many issues with the code you are using to convert decimal to binary. If you are insistent on not using a built-in function for that purpose, you may be interested by this post:
Convert an integer to binary without using the built-in bin function
Although personally if I were to want to avoid the bin() function, I would do something like:
"{0:#b}".format(an_integer)
Demo:
>>> "{0:#b}".format(22)
'0b10110'
This is much more Pythonic than your current code.
this would be a lot easier no?
conv = str(input('what do you want to convert?'))
if conv == 'binary':
x = str(input('enter binary : '))
ansBin = int(x, 2)
print('decimal for binary ', x, ' is ', ansBin)
elif conv == 'decimal':
y = int(input('enter decimal : '))
ansDec = bin(y)[2:]
print('binary for decimal ', y, ' is ', ansDec)
You can use int and 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 int like that, then you could 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 demonstration:
>>> binary = raw_input('enter a number: ')
enter a number: 11001
>>> decimal = 0
>>> for digit in binary:
... decimal = decimal*2 + int(digit)
...
>>> print decimal
25
>>>
Binary to Decimal
int(binaryString, 2)
Decimal to Binary
format(decimal ,"b")
ps: I understand that the author doesn't want a built-in function. But this question comes up on the google feed even for those who are okay with in-built function.