This article will show you how to use Python to convert decimal numbers to binary, octal, hexadecimal and other bases up to base 36 and vice versa.
Convert Decimal number to any other base in Python
Table of Contents
- Introduction
- Base-16 number
- Base-36 number
- Python built-in functions for number system conversion
- Decimal to Binary
- Decimal to Octal
- Decimal to Hexadecimal
- Decimal to ANY base-k where k is between 2 and 36
- Binary to Decimal
- Number in any base-k [where k is between 2 and 36] to Decimal
- Functions to convert decimal to any base, and any base to decimal
- Number System Practice Exercises book
Introduction
If you have worked on number systems in mathematics or computer science, you know that there are different number systems, including decimal, binary, octal and hexadecimal. There are built-in functions to convert from decimal to binary, octal and hexadecimal, but not for other bases. For example, if you want to convert the number 25 to base 12, you have to write that part of the code from scratch.
This blog post shows you how to write Python code to convert from base-10 to any base up to base-36 and vice versa.
Base-16 number
A base-16 number or a hexadecimal number is a number consisting of the digits 0-9 and A-F. There are 16 hexadecimal digits in base-16 numbers.
Base-36 number
A base-36 number is a number consisting of the digits 0-9 and A-Z. There are 36 digits in base-36 numbers.
Python built-in functions for number system conversion
Python has built-in functions to covert from:
- decimal to binary bin()
- decimal to octal oct()
- decimal to hexadecimal hex()
- binary to decimal 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 9 in binary is 10012. To convert 9 from decimal to binary:
i = 9
bin_i = bin(i)
print(bin_i[2:])
OUTPUT:
1001
This binary representation bin(9)
is 0b1001
. To get rid of the leading 0b
, we slice the string from index 2 onwards to get the number we want, in string format.
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 23 is 278. To convert 23 from decimal to octal:
i = 23
oct_i = oct(i)
oct_i = oct(i)[2:]
print(oct_i)
OUTPUT:
27
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 36 is 2416. To convert 36 from decimal to hexadecimal:
i = 36
hex_i = hex(i)[2:]
print(hex_i)
OUTPUT:
24
Decimal to ANY base-k where k is between 2 and 36
There is no built-in function to convert a decimal number to a base other than 2, 8 or 16.
We will write our own function for this. Let us call it decimal_to_base(n, k)
.
It will accept two input arguments, the number n
and the base k
to which it will be converted to.
# Function to convert decimal number to any base <= 36
def decimal_to_base(n, k):
digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
nk = ''
while n:
nk += digits[n % k]
n //= k
return nk[::-1]
# Main function
n = 29 # decimal number
k = 20 # base
nk = decimal_to_base(n, k)
print(nk)
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 11112 is 15. This code will convert 11112 from binary to decimal:
x = '1111'
i = int(x, 2)
print(i)
OUTPUT:
15
Number in any base-k [where k is between 2 and 36] to Decimal
To convert a number greater than base-1 and lesser than or equal to base-36, we use int()
with the first number in base-k as the first input parameter and k
as the second parameter. The function will return the decimal value.
Base-12 number to decimal
For example, number 3A12 is 46. This code will convert 3A12 from base-12 to decimal:
x = '3A'
i = int(x, 12)
print(i)
OUTPUT:
46
Base-16 number to decimal
To convert hexadecimal number FA16 to its decimal equivalent 250, this code will do the job:
x = 'FC'
i = int(x, 16)
print(i)
OUTPUT:
252
Base-36 number to decimal
Another example, number XY36 is 1222.
x = 'XY'
i = int(x, 36)
print(i)
OUTPUT:
1222
Functions to convert decimal to any base, and any base to decimal
Putting together two functions to convert decimal to any base, and vice versa:
# Function to convert decimal number to any base <= 36
def decimal_to_base(n, k):
digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
nk = ''
while n:
nk += digits[n % k]
n //= k
return nk[::-1]
# Function to convert any base to decimal
def base_to_decimal(n, l):
return int(n, k)
# Main function
n = 255
k = 16
print(decimal_to_base(n, k))
n = '100'
k = 36
print(base_to_decimal(n, k))
OUTPUT:
FF
1296
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.
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.