Advertisement
AceScottie

int to binary conversion

Apr 21st, 2019
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. def pad(x, count): ##Adds leading 0s to the start of the string to match the count given
  2.     while len(x) < count:
  3.         x = ("0%s"%x)
  4.     return x
  5. def bit_count(x): ##returns a standard binary count number (1, 2, 4, 8, 16, 32, 64 etc...)
  6.     bits = 1;
  7.     while bits < len(x):
  8.         bits = bits*2
  9.     return bits
  10. def bin_to_int(x): ##converts binary string to int
  11.     return int(x, 2)
  12.    
  13. byte = "{0:b}".format(int(raw_input("Enter Number:"))) ##converts int input into binary string
  14. print("This is a %s bit number." %bit_count(byte))
  15. print(pad(byte, bit_count(byte)))
  16. print(bin_to_int(byte))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement