TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Convert Decimal Binary Octal and Hexadecimal

Python Convert Decimal Binary Octal and Hexadecimal for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.

<< Back to PYTHON

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:

Input: 64
Output: 
         64 in Binary: 0b1000000
         64 in Octal:  0o100
         64 in Hexadecimal:  0x40

Input: 312
Output: 
         312 in Binary:  0b100111000
         312 in Octal: 0o470
         312 in Hexadecimal:  0x138

Code:

# First, we will define the function to convert decimal to binary
def decimal_into_binary(decimal_1):
    decimal = int(decimal_1)
  
    # then, print the equivalent decimal
    print ("The given decimal number", decimal, "in Binary number is: ", bin(decimal))
# we will define the function to convert decimal to octal
def decimal_into_octal(decimal_1):
    decimal = int(decimal_1)
  
    # Then, print the equivalent decimal
    print ("The given decimal number", decimal, "in Octal number is: ", oct(decimal))
# we will define the function to convert decimal to hexadecimal
def decimal_into_hexadecimal(decimal_1):
    decimal = int(decimal_1)
  
    # Then, print the equivalent decimal
    print ("The given decimal number", decimal, " in Hexadecimal number is: ", hex(decimal))
  
# Driver program
decimal_1 = int (input (" Enter the Decimal Number: "))
decimal_into_binary(decimal_1)
decimal_into_octal(decimal_1)
decimal_into_hexadecimal(decimal_1)

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.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf