Convert Binary, Octal, Decimal and Hexadecimal Numbers using Python

Published January 24, 2024

If you have worked on number systems in mathematics or computer science, you know that the four most common number systems are binary (base-2), octal (base-8), decimal (base-10) and hexadecimal (base-16). This article will show you how to use Python to convert back and forth between these four number systems.

Convert Between Binary, Octal, Decimal and Hexadecimal Numbers in Python

Introduction

The binary number system is also called the base-2 number system. The valid digits (or bits) are 0 and 1.

The octal number system is also called the base-8 number system. The valid digits (or bits) are 0, 1, 2, 3, 4, 5, 6, 7.

The decimal number system is also called the base-10 number system and this is what we use in day to day life. The valid digits (or bits) are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

The hexadecimal number system is also called the base-16 number system. The valid digits (or bits) are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.

Since we have four number systems, there are 12 types of conversions between them.

Binary, Octal, Decimal and Hexadecimal Numbers

Python functions used for number system conversion

The functions you will be using are bin(), oct(), hex(), int(). You will also be using string slicing. The decimal number will be of the int data type while the binary, octal and hexadecimal numbers will be of string data type.

Decimal to Binary

In Python, binary numbers are represented by adding the prefix 0b. To convert a decimal number to binary, we use the bin() function and pass in the decimal number as input parameter.

For example, decimal 6 in binary is 1102. To convert 6 from decimal to binary:

i = 6
bin_i = bin(i)
print(bin_i)

OUTPUT:

0b110

This binary representation is of type string. You can check it with type(bin_i). To get rid of the leading 0b, we can use string slicing, and grab from index 2 onwards. Our code now becomes:

i = 6
bin_i = bin(i)[2:]
print(bin_i)

OUTPUT:

110

Decimal to Octal

In Python, octal numbers are represented by adding the prefix 0o. To convert a decimal number to octal, we use the oct() function and pass in the decimal number as input parameter.

For example, decimal 13 is 158. To convert 13 from decimal to octal:

i = 13
oct_i = oct(i)
print(oct_i)

OUTPUT:

0o15

To get rid of the leading 0o:

i = 13
oct_i = oct(i)[2:]
print(oct_i)

OUTPUT:

110

Decimal to Hexadecimal

In Python, hexadecimal numbers are represented by adding the prefix 0x. To convert a decimal number to hexadecimal, we use the hex() function and pass in the decimal number as input parameter.

For example, decimal 25 is 1916. To convert 25 from decimal to hexadecimal:

i = 25
hex_i = hex(i)
print(hex_i)

OUTPUT:

0x19

To get rid of the leading 0x:

i = 25
hex_i = hex(i)[2:]
print(hex_i)

OUTPUT:

19

Binary to Decimal

Now, to convert a binary number, stored as a string, to decimal, we use int() with the binary number as the first input parameter and the base (2) as the second parameter. The function will return the decimal value.

For example, binary number 11102 is 14. This code will convert 11102 from binary to decimal:

x = '1110'
i = int(x, 2)
print(i)

OUTPUT:

14

That was easy and obviously, quite flexible.

Octal to Decimal

To convert an octal number to decimal, we use int() with the octal number as the first input parameter and 8 as the second parameter. The function will return the decimal value.

For example, octal number 1778 is 127. This code will convert 1778 from octal to decimal:

x = '177'
i = int(x, 8)
print(i)

OUTPUT:

127

Hexadecimal to Decimal

To convert a hexadecimal number to decimal, we use int() with the hexadecimal number as the first input parameter and 16 as the second parameter. The function will return the decimal value.

For example, hexadecimal number FA16 is 250. This code will convert FA16 from hexadecimal to decimal:

x = 'FA'
i = int(x, 16)
print(i)

OUTPUT:

250

Binary to Octal

To convert a binary number x to octal, we call int(x, 2) and call oct() over the returned value to get the octal number.

For example, 1100112 = 638. Let us write code for that:

x = '110011'
oct_x = oct(int(x, 2))[2:]
print(oct_x)

OUTPUT:

63

Binary to Hexadecimal

To convert a binary number x to hexadecimal, we call int(x, 2) and call hex() over the returned value to get the hexadecimal number. If the hexadecimal number contains digits A through F, they will be shown in lowercase. To convert them to uppcase, add the method .upper() to the result.

For example, 110100112 = D316. Let us write code for that:

x = '11010011'
hexa_x = hex(int(x, 2))[2:].upper()
print(hexa_x)

OUTPUT:

D3

Octal to Hexadecimal

To convert an octal number x to hexadecimal, we call int(x, 8) and call hex() over the returned value to get the hexadecimal number. If the hexadecimal number contains digits A through F, they will be shown in lowercase. To convert them to uppcase, add the method .upper() to the result.

For example, 3478 = E716. Let us write code for that:

x = '347'
hexa_x = hex(int(x, 8))[2:].upper()
print(hexa_x)

OUTPUT:

E7

Hexadecimal to Octal

To convert a hexadecimal number x to octal, we call int(x, 16) and call oct() over the returned value to get the octal number.

For example, A016 = 2408. Let us write code for that:

x = 'A0'
oct_x = oct(int(x, 16))[2:]
print(oct_x)

OUTPUT:

240

Hexadecimal to Binary

To convert a hexadecimal number x to binary, we call int(x, 16) and call bin() over the returned value to get the binary number.

For example, A016 = 101000002. Let us write code for that:

x = 'A0'
bin_x = bin(int(x, 16))[2:]
print(bin_x)

OUTPUT:

10100000

Octal to Binary

To convert an octal number x to binary, we call int(x, 8) and call oct() over the returned value to get the octal number.

For example, 458 = 1001012. Let us write code for that:

x = '45'
bin_x = bin(int(x, 8))[2:]
print(bin_x)

OUTPUT:

100101

Number System Practice Exercises book

We are also selling our Number Systems Practice Exercise book on Amazon, if you are interested to practice your number conversions on paper.

Number Systems Practice Exercises Book

This book contains over a thousand questions and is useful for a number of math classes, and after-school clubs like the American Computer Science League (ACSL) and USACO clubs.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: January 24, 2024.     This post was originally written on January 24, 2024.