Python To convert decimal to binary, octal, hexadecimal

Python To convert decimal to binary, octal, hexadecimal

The decimal system is one of the most commonly used number system. However, computers only understand binary. Binary, octal and hexadecimal number systems are closely related and need to convert the decimal into these systems.


The decimal system is base 10 (ten symbols, 0-9, used to denote a number) and similarly, binary base 2, octal base 8 and hexadecimal base 16.

A number with the prefix 0b is considered binary, 0o is considered an octal and 0x is considered hexadecimal. For example:

86 = 0b 1010110 = 0o126 = 0x56

Now take a look at the code how to write a program to convert decimal to binay, octal and hexadecimal.

Python Code:
a = int(input('enter any number'))
b = bin(a)
print("decimal to binary:",b)
c = oct(a)
print("decimal to octal:",c)
d = hex(a)
print("decimal to hexadecimal:",d)
Output:
enter any number86
decimal to binary: 0b1010110
decimal to octal: 0o126
decimal to hexadecimal: 0x56