bobhig

Binary conversions

Apr 11th, 2019
176
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Denary to Binary Conversion
  2.  
  3. value = 0
  4. binary_list = []
  5. binary_digit = ""
  6.  
  7. value = int(input("input your denary number "))
  8.  
  9. while value > 0:
  10.     binary_digit = str(value%2)
  11.     binary_list.insert(0, binary_digit)
  12.     value=value//2
  13.  
  14. print("Binary number = ", "".join(binary_list))
  15.  
  16. # Binary to Denary conversion
  17.  
  18. binary = input("Input a number in binary: ")  
  19. denary = 0  
  20. for digit in binary:    
  21.   denary = denary*2 + int(digit)
  22.  
  23. # or alternatively....
  24.  
  25. denary2 = int(binary, 2) #just convert a base 2 number to an integer
  26.  
  27. print("Your denary number is: " + str(denary), "\n")
  28. print("Your denary number is:", denary2)
RAW Paste Data