Convert Decimal Numbers in Python with bin(), oct(), and hex()
Convert Decimal Numbers in Python with bin(), oct(), and hex() ๐ข
Need to convert numbers to different bases? Python makes it easy with its built-in
bin(), oct(), and hex() functions.
Whether you're debugging, working with binary protocols, or just exploring, these tools are essential.
✅ Decimal → Binary: bin()
# Convert decimal to binary
print(bin(10)) # 0b1010
✅ Decimal → Octal: oct()
# Convert decimal to octal
print(oct(10)) # 0o12
✅ Decimal → Hexadecimal: hex()
# Convert decimal to hexadecimal
print(hex(255)) # 0xff
✅ Format output without prefixes
# Remove prefix and format output
print(bin(10)[2:]) # '1010'
print(hex(255)[2:].upper()) # 'FF'
These conversion functions are useful when formatting low-level data, debugging binary operations, or working on system-level tasks.
Decimal is just one perspective. Explore binary, octal, and hex to see the full picture! ๐ง
Icons by Flaticon
Comments
Post a Comment