Here we will write a Python program to convert Decimal to Binary, Octal and Hexadecimal number systems.
How to convert Decimal to Binary, Octal and Hexadecimal?
Good thing is in Python we have built-in functions for these conversions. In our program we will use built-in functions bin(), oct() and hex() to convert the given decimal number into respective number systems.
These functions take an integer (in decimal) and return a string.
Now lets write a simple program for the same.
# Python program to convert decimal number into binary, octal and hexadecimal number system # We can change this value to check for different numbers dec = 344 print("The decimal value of",dec,"is:") print(bin(dec),"in binary.") print(oct(dec),"in octal.") print(hex(dec),"in hexadecimal.")
The decimal value of 344 is: 0b101011000 in binary. 0o530 in octal. 0x158 in hexadecimal.
A number with the prefix ‘0b’ is considered binary, ‘0o’ is considered octal and ‘0x’ as hexadecimal. For example:
60 = 0b11100 = 0o74 = 0x3c
Please comment if you face any problem or concerns.