convert base 10 to binary python code example
Example 1: convert int to binary python
def decimalToBinary(n):
return bin(n).replace("0b", "")
if __name__ == '__main__':
print(decimalToBinary(8))
print(decimalToBinary(18))
print(decimalToBinary(7))
Output:
1000
1001
Example 2: how to convert decimal to binary python
a = 5
print(bin(a))
Example 3: decimal to binary in python
a = 10
bnr = bin(a).replace('0b','')
x = bnr[::-1]
while len(x) < 8:
x += '0'
bnr = x[::-1]
print(bnr)
Example 4: convert binary string to base 10 value in python
def getBaseTen(binaryVal):
count = 0
binaryVal = binaryVal[::-1]
for i in range(0, len(binaryVal)):
if(binaryVal[i] == "1"):
count += 2**i
return count