Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

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

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?

Answer*

Draft saved
Draft discarded
Cancel
4
  • Thanks for the comment and link. If you were to post code, it may be clear by now that it probably wouldn't do me much good other than copy and pasting. Thank you again, and I will read into the wiki page. Commented Nov 15, 2017 at 23:54
  • So I did run into the formula for converting to decimal which i had found before. The other day I got lost in trying to use the "len()" of the user input and emulating the formula for conversion. It was overwhelming. Commented Nov 16, 2017 at 0:18
  • I’m having trouble checking whether or not the user input is binary or not. Is there a way to implement that before the formula? Commented Nov 19, 2017 at 1:08
  • I've added some code with description. Let me know if it helps. Commented Nov 19, 2017 at 8:35

lang-py

AltStyle によって変換されたページ (->オリジナル) /