import string def posbincon(userInput): if userInput == 0: return 0 binaryString = '' while userInput > 0: j = userInput & 1 binaryString = str(j) + binaryString userInput >>= 1 return binaryString def negbincon(userInput): result = list(posbincon(0-userInput)) con = False bit = len(result) while bit > 0: if con is False and result[bit-1] is '1': con = True elif con is True: if result[bit-1] is '0': result[bit-1] = '1' elif result[bit-1] is '1': result[bit-1] = '0' bit -= 1 result = '1'+string.join(result, '') return result print "Welcome to the Ultimate, low budget binary to decimal converter" print "Now including Two's Complement" userInput = str while type(userInput) is not int: userInput = raw_input("Enter the number here:> ") try: userInput = int(userInput) except: print "Input must be an integer." if userInput >= 0: print "The binary value of %s is %s" %(userInput, posbincon(userInput)) else: print "The binary value of %s is %s" %(userInput, negbincon(userInput))