Binary Integer

created:

updated:

tags: python binary number

Convert a binary number in string format to an integer (decimal-based)

if a = "101", we can get the integer number in decimal based by int(a, 2).

a = "101"
print(int(a, 2))
# prints 5

Convert a decimal-based integer to a binary representation in string

if a = 5, then the integer can be represented as a binary string through f"{a:b}"

a = 5
a_binary = f"{a:b}"
print(a_binary)
# prints 101