BrandonSpendlove

Simple hex to binary practice script

Oct 7th, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. import time
  2.  
  3. default_timer = 2 #Change default time added for each hex value you want to convert
  4.  
  5. print "Each hex character will add {} seconds on the timer...".format(default_timer)
  6. inputHex = raw_input('Input Hex Value: ')
  7.  
  8. calc_time = default_timer * len(inputHex) #Calculate how much time for user to calculate binary
  9.  
  10. bConvert = bin(int(inputHex, 16)) #Convert to hex
  11. bits = len(bConvert[2:]) #Get length to pad zeros if needed..
  12. bConvertNew = bConvert[2:].zfill(bits) #New variable because converting hex will also add 0b at the
  13.  
  14. #Space out the binary every 4 bits (so we can see each hex in binary)
  15. spacedConvert = [bConvertNew[i:i+4] for i in range(0, len(bConvertNew), 4)] #Will convert to list
  16. spacedConvert = ' '.join([bConvertNew[i:i+4] for i in range(0, len(bConvertNew), 4)]) #Rejoin list with space in between the binary values...
  17.  
  18. print "Start...You have {} seconds to convert\n\n".format(calc_time)
  19. time.sleep(calc_time)
  20.  
  21. print spacedConvert
Advertisement
Add Comment
Please, Sign In to add comment