Converting decimal numbers into binary, octal, and hexadecimal can be done using various programming languages. In this article, we will discuss a Python program to convert decimal numbers into binary, octal, and hexadecimal.
Why Convert Decimal Numbers to Binary, Octal, and Hexadecimal?
Before we dive into the Python program, let’s quickly discuss why we would want to convert decimal numbers to binary, octal, or hexadecimal. The primary reason is that these number systems are commonly used in computer science and digital electronics. Binary, for example, is the most basic number system in digital electronics and is used to represent the “on” and “off” states of electronic switches.
Hexadecimal, on the other hand, is used to represent large numbers in a compact and convenient way. For example, the decimal number 255 can be represented as FF in hexadecimal notation. Octal is another commonly used number system, although it is less popular than binary and hexadecimal.
Converting Decimal to Binary in Python
Converting decimal to binary in Python is easy using the built-in function “bin()”. The “bin()” function takes a decimal number as input and returns its binary equivalent.
decimal_num = 10
binary_num = bin(decimal_num)
print("Binary equivalent of", decimal_num, "is", binary_num)
Output:
Binary equivalent of 10 is 0b1010
As you can see, the “bin()” function returns a binary string prefixed with “0b”. We can remove the prefix using string slicing to get the binary equivalent without the prefix.
binary_num = bin(decimal_num)[2:]
print("Binary equivalent of", decimal_num, "is", binary_num)
Output:
Binary equivalent of 10 is 1010
Converting Decimal to Octal in Python
Converting decimal to octal in Python is easy using the built-in function “oct()”. The “oct()” function takes a decimal number as input and returns its octal equivalent.
decimal_num = 10
octal_num = oct(decimal_num)
print("Octal equivalent of", decimal_num, "is", octal_num)
Output:
Octal equivalent of 10 is 0o12
Similar to the “bin()” function, the “oct()” function returns an octal string prefixed with “0o”. We can remove the prefix using string slicing to get the octal equivalent without the prefix.
octal_num = oct(decimal_num)[2:]
print("Octal equivalent of", decimal_num, "is", octal_num)
Output:
Octal equivalent of 10 is 12
Converting Decimal to Hexadecimal in Python
Converting decimal to hexadecimal in Python is easy using the built-in function “hex()”. The “hex()” function takes a decimal number as input and returns its hexadecimal equivalent.
decimal_num = 10
hexadecimal_num = hex(decimal_num)
print("Hexadecimal equivalent of", decimal_num, "is", hexadecimal_num)
Output:
Hexadecimal equivalent of 10 is 0xa
Similar to the “bin()” and “oct()” functions, the “hex()” function returns a hexadecimal string prefixed with “0x”. We can remove the prefix using string slicing to get the hexadecimal equivalent without the prefix.
hexadecimal_num = hex(decimal_num)[2:]
print("Hexadecimal equivalent of", decimal_num, "is", hexadecimal_num)
Output:
Hexadecimal equivalent of 10 is a
Thanks for reading. Happy coding!