Binary, Octal and Hexadecimal Values in Python
Daniel Nogueira
Posted on December 3, 2022
Converting a decimal number to its binary, octal or hexadecimal value is easier than it looks. A simple way to do this is using the bin
, oct
and hex
functions directly:
n = 97
print(bin(n))
print(oct(n))
print(hex(n))
Result:
0b1100001
0o141
0x61
Note that we have two digits of standardization at the beginning of the results, to solve this, we can slice it as follows:
n = 97
print(bin(n)[2:])
print(oct(n)[2:])
print(hex(n)[2:])
Result:
1100001
141
61
💖 💪 🙅 🚩
Daniel Nogueira
Posted on December 3, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.