Advertisement
jabela

Twos Complement to Denary

Aug 24th, 2017
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. twos = input("Please Enter an 8 digit Twos Complement Number")
  2. if len(twos)!=8:
  3.     print("Invalid length")
  4.     #return
  5. print(len(twos))
  6. for i in twos:
  7.         if int(i) > 1:
  8.             print("invalid value")
  9.             #return
  10. if twos[0]=="1":
  11.     print("negative twos complement")
  12.     twos = int(twos, 2)
  13.     decimal = 256 - twos
  14.     print("decimal: -",decimal)
  15.     hexa=(hex(decimal))
  16.     print("Hex: -",hexa[2::])
  17. else:
  18.     print("positive twos complement")
  19.     number = int(twos,2)
  20.     print("decimal:",number)
  21.     hexa=(hex(number))
  22.     print("Hex:",hexa[2::])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement