Matuiss2

binary - decimal(general)

Sep 18th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.33 KB | None | 0 0
  1. # Input
  2. bin = int(input("Type a binary number"))
  3. # Process
  4. str_bin = str(bin)
  5. power = 0
  6. lengh = len(str_bin)
  7. total = 0
  8. while lengh  > 0:
  9.     unit = str_bin[lengh-1: lengh]
  10.     int_unit = int(unit)
  11.     total += int_unit * 2 ** power
  12.     power += 1
  13.     lengh -= 1
  14. print("The decimal value for this binary number is", total)
Advertisement
Add Comment
Please, Sign In to add comment