Task: Python Convert Decimal to Binary Octal Hexadecimal
How this Program Works?
This Python program will perform number convertion using builtin Python functions.
It uses builtin Python functions for number system conversion:
- bin(int_number)
- oct(int_number)
- hex(int_number)
Python bin() Function
The Python bin() function converts an integer number to a binary string prefixed with 0b . For example, the binary equivalent of 2 is 0b10 .
Python oct() Function
The oct() function converts an integer into an octal string.
Octal strings in Python are prefixed with 0o.
Example:
x = oct(12)
print(x)
Output: 0o14
Python hex() Function
The hex() function converts the specified number into a hexadecimal value. The returned string always starts with the prefix 0x .
Example:
x = hex(255)
print(x)
Output: 0xff
Source Code Python Convert Decimal to Binary, Octal, and Hexadecimal numbers
# this Python program converts a given # decimal number to corrsponding # binary, octal and hexadecimal # numbers using builtin functions dec = int(input("Enter a decimal number: ")) print(bin(dec),"in binary.") print(oct(dec),"in octal.") print(hex(dec),"in hexadecimal.")
Sample Run Output: Enter a decimal number: 9 0b1001 in binary. 0o11 in octal. 0x9 in hexadecimal. Sample Run 2: Enter a decimal number: 145 0b10010001 in binary. 0o221 in octal. 0x91 in hexadecimal.