Python Base Conversion Functions
In this tutorial, we are going to build a clear and structured understanding of base conversion in Python, which is an essential concept when working with numbers in different number systems such as binary, octal, decimal, and hexadecimal. While the idea may initially remind you of school-level mathematics, Python makes these conversions extremely simple and practical through built-in functions.
1. Understanding Number Systems
Before jumping into Python functions, it is important to briefly understand what we mean by number systems.
- Decimal (Base 10): The standard number system we use in daily life (0–9).
- Binary (Base 2): Used internally by computers (0 and 1).
- Octal (Base 8): Uses digits from 0–7.
- Hexadecimal (Base 16): Uses digits 0–9 and letters A–F.
In real-world programming, especially in low-level operations, networking, memory management, and debugging, these number systems are used frequently.
2. Base Conversion in Python
Python provides three built-in functions that make base conversion extremely straightforward:
bin()→ Converts a number to binaryoct()→ Converts a number to octalhex()→ Converts a number to hexadecimal
These functions allow you to convert a number from decimal or other bases into the required format without manually performing calculations.
3. Binary Conversion using bin()
The bin() function is used to convert a number into its binary representation.
Example 1: Decimal to Binary
print(bin(15)) # 0b1111
- The prefix 0b indicates that the number is in binary format.
- The value 1111 is the binary equivalent of decimal 15.
Example 2: Octal to Binary
print(bin(0o123))
Example 3: Hexadecimal to Binary
print(bin(0xFace))
Here, Python automatically understands the input base because of the prefixes:
0o→ Octal0x→ Hexadecimal
So, the bin() function can convert numbers from any base into binary, as long as the input is an integer.
4. Octal Conversion using oct()
The oct() function converts a number into its octal representation.
Example 1: Decimal to Octal
print(oct(100)) # 0o144
Example 2: Binary to Octal
print(oct(0b111101))
Example 3: Hexadecimal to Octal
print(oct(0xFace))
- The prefix
0oindicates that the result is in octal format. - This function is useful when dealing with systems where octal representation is preferred, such as file permissions in Unix systems.
5. Hexadecimal Conversion using hex()
The hex() function converts a number into its hexadecimal representation.
Example 1: Decimal to Hexadecimal
print(hex(1000)) # 0x3e8
Example 2: Binary to Hexadecimal
print(hex(0b11010111111))
Example 3: Octal to Hexadecimal
print(hex(0o123456))
- The prefix 0x indicates hexadecimal format.
- Hexadecimal numbers are widely used in memory addressing, color codes (e.g., #FF5733), and low-level programming.
6. Important Observations
6.1 These Functions Work Only with Integers
All three functions—bin(), oct(), and hex()—work only with integral values.
print(bin(10.5)) # This will throw an error
So, if you are working with floating-point numbers, you must first convert them into integers.
6.2 No Function Needed for Decimal Conversion
A common question arises:
“If we have functions for binary, octal, and hexadecimal, where is the function for decimal?”
The answer is simple:
- Decimal is Python’s default number system.
- If you do not use any conversion function, the number is already treated as decimal.
print(100) # This is already decimal
6.3 Output is Always a String
All these functions return the result in string format, not as numeric values.
result = bin(10)
print(type(result)) # <class 'str'>
This means if you need numeric operations, you must convert it back using int().
7. Converting Back to Integer
To convert a binary, octal, or hexadecimal string back into decimal, you can use:
int("1010", 2) # Binary to decimal
int("144", 8) # Octal to decimal
int("3e8", 16) # Hexadecimal to decimal