python code to convert a number to binary code example
Example 1: python int to binary
print('{0:b}'.format(3)) # '11'
print('{0:8b}'.format(3)) # ' 11'
print('{0:08b}'.format(3)) # '00000011'
def int2bin(integer, digits):
if integer >= 0:
return bin(integer)[2:].zfill(digits)
else:
return bin(2**digits + integer)[2:]
print(int2bin(3, 6)) # '000011'
Example 2: python binary representation of numbers
# get integer version of binary number
int(bin(8)[2:])