Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Binary to Decimal in python (without built in binary function)

Tags:

python

binary

Alrighty, first post here, so please forgive and ignore if the question is not workable;

Background: I'm in computer science 160. I haven't taken any computer related classes since high school, so joining this class was a big shift for me. It all seemed very advanced. We have been working in Python and each week we are prompted to write a program.

I have been working with this problem for over a week and am having a hard time even starting.
The prompt is to read an integer containing only 1's and 0's, process the binary number digit by digit and report the decimal equivalent. Now, I have gotten some tips from a classmate and it sent me at least in a direction.

Set up a couple of counters; using the % operator to check the remainder of the number divided by 2, and slicing off the last number (to the right) to move on to and process the next digit.

I am having an incredibly hard time wrapping my head around what formula to use on the binary digits themselves which will convert the number to decimal.

setbitval = 0
counter = 0

user = int(input("enter a binary value. "))

if user % 2 == 1:
        user = (user/10) - .1
        setbitval += 1

This is all I've got so far.. My thinking is getting in the way. I've searched and searched, even through these forums.

Any information or thoughts are extremely appreciated,
T

Edit: okay guys, everyone's help has been extremely useful but I'm having a problem checking if the user input is not a binary number.

for i in reversed(bits):   
decimal += 2**counter * int(i)

counter += 1    

This is the formula someone here gave me and I've been trying different iterations of "for i in bits: if i in bits: != 0 or 1" and also "if i in bits: >= 1 or <=0".
Any thoughts?

like image 969
relytkadReh Avatar asked Aug 24 '26 09:08

relytkadReh


2 Answers

you can use this code:

binary= raw_input("Binary: ")
d= int(binary, 2)
print d
like image 149
apk Avatar answered Aug 26 '26 00:08

apk


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
like image 44
user3362334 Avatar answered Aug 25 '26 22:08

user3362334



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!