Home » Convert Decimal to Binary, Octal and Hexadecimal using python

Convert Decimal to Binary, Octal and Hexadecimal using python

In this article we show you how to convert a decimal number into binary, octal, and hexadecimal numbers.

Here are a brief description of the common systems

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

Binary System: The binary system is a base 2 number system. The binary system is used because computers only understand binary numbers. In this system, two numbers (0-1) are used to represent a number.

Octal System: Octal system is a base 8 number system. In this system, eight numbers (0-7) are used to represent a number.

Hexadecimal System: Hexadecimal system is base 16 number system. Hexadecimal uses the decimal numbers and six extra symbols. There are no numerical symbols that represent values greater than nine, so the letters used are A, B, C, D, E and F. Hexadecimal A = decimal 10, and hexadecimal F = decimal 15.

Table of examples

 Binary Numbers Octal Numbers Decimal Numbers Hexadecimal Numbers 0000 0 0 0 0001 1 1 1 0010 2 2 2 0011 3 3 3 0100 4 4 4 0101 5 5 5 0110 6 6 6 0111 7 7 7 1000 10 8 8 1001 11 9 9 1010 12 10 A 1011 13 11 B 1100 14 12 C 1101 15 13 D 1110 16 14 E 1111 17 15 F

Code example

In this example we will create functions for converting from decimal to the other number systems

We will use built in functions for doing this, these are: bin() (for binary), oct() (for octal), and hex() (for hexadecimal) . These functions take an integer and return a string.

Lets see the example, save the following as baseconversion.py

```# function to convert decimal to binary
def dec_to_bin(myNumber):
decimal = int(myNumber)
# then, print the equivalent decimal
print ("The given decimal number", decimal, "in Binary number is: ", bin(decimal))

# function to convert decimal to octal
def dec_to_oct(myNumber):
decimal = int(myNumber)
# print the equivalent decimal
print ("The given decimal number", decimal, "in Octal number is: ", oct(decimal))

# function to convert decimal to hexadecimal
def dec_to_hex(myNumber):
decimal = int(myNumber)
# print the equivalent decimal
print ("The given decimal number", decimal, " in Hexadecimal number is: ", hex(decimal))

# test this all out
myNumber = int (input (" Enter the Decimal Number: "))
dec_to_bin(myNumber)
dec_to_oct(myNumber)
dec_to_hex(myNumber)
```

Lets see a few runs of this code

```>>> %Run baseconversion.py
Enter the Decimal Number: 16
The given decimal number 16 in Binary number is: 0b10000
The given decimal number 16 in Octal number is: 0o20
The given decimal number 16 in Hexadecimal number is: 0x10
>>> %Run baseconversion.py
Enter the Decimal Number: 255
The given decimal number 255 in Binary number is: 0b11111111
The given decimal number 255 in Octal number is: 0o377
The given decimal number 255 in Hexadecimal number is: 0xff
>>> %Run baseconversion.py
Enter the Decimal Number: 8
The given decimal number 8 in Binary number is: 0b1000
The given decimal number 8 in Octal number is: 0o10
The given decimal number 8 in Hexadecimal number is: 0x8```