Home » Python Convert Decimal Binary Octal and Hexadecimal

Python Convert Decimal Binary Octal and Hexadecimal

by Online Tutorials Library

Python Program to Convert Decimal to Binary, Octal and Hexadecimal

In this tutorial, we will discuss how we can convert the decimal number into binary, octal, and hexadecimal numbers.

Decimal System: The most widely used number system is decimal system. This system is base 10 number system. In this system, ten numbers (0-9) are used to represent a number.

Binary System: Binary system is base 2 number system. Binary system is used because computers only understand binary numbers (0 and 1).

Octal System: Octal system is base 8 number system.

Hexadecimal System: Hexadecimal system is base 16 number system.

This program is written to convert decimal to binary, octal and hexadecimal.

We have a number in a decimal number, and we have to convert it into a binary, octal, and hexadecimal number. We will use a function for converting decimal numbers to binary numbers, decimal numbers to octal numbers, and decimal numbers to hexadecimal numbers.

Examples:

Code:

Output:

Case – (1):

Enter the Decimal Number:  12  The given decimal number 12 in Binary number is:  0b1100  The given decimal number 12 in Octal number is:  0o14  The given decimal number 12 in Hexadecimal number is:  0xc  

Case – (2):

Enter the Decimal Number:  196  The given decimal number 196 in Binary number is:  0b11000100  The given decimal number 196 in Octal number is:  0o304  The given decimal number 196 in Hexadecimal number is:  0xc4  

Explanation:

In the above program, we have used the inbuilt functions: bin() (for binary), oct() (for octal), and hex() (for hexadecimal) for converting the given decimal number into respective number systems. These functions take an integer and return a string.

Conclusion

In this tutorial, we have discussed how we can use the inbuilt functions of Python for converting decimal numbers into binary, octal, and hexadecimal number.


You may also like