Beyond Base 10: Converting Numbers to Hexadecimal and Octal
Introduction: More Than Just 0s and 1s
While the binary (base-2) system is fundamental to computers, and decimal (base-10) is what we use daily, other number systems play important roles in computing, particularly hexadecimal (base-16) and octal (base-8). These systems offer more compact ways to represent binary data, making them useful in areas like memory addressing, color codes, and file permissions.
In our previous post on binary/decimal conversion, we established the foundation of place values based on powers of the base. Let's extend that understanding.
Octal (Base-8)
The octal system uses eight digits: 0, 1, 2, 3, 4, 5, 6, and 7. Each position represents a power of 8.
Converting Octal to Decimal: Similar to binary, multiply each digit by the corresponding power of 8.
Example: Convert to decimal.
Converting Decimal to Octal: Repeatedly divide the decimal number by 8 and record the remainders. Read remainders in reverse order.
Example: Convert to octal.
- remainder 2
- remainder 7
- remainder 3
Reading bottom-up: 372. So, .
Relationship with Binary: Since , each octal digit corresponds directly to three binary digits:
- 0 = 000
- 1 = 001
- 2 = 010
- 3 = 011
- 4 = 100
- 5 = 101
- 6 = 110
- 7 = 111
To convert binary to octal, group binary digits in threes from right to left (adding leading zeros if needed) and convert each group. To convert octal to binary, replace each octal digit with its three-digit binary equivalent.
Example: Convert to octal.
Group: 10 110 110 -> 010 110 110
Convert: 010 = 2, 110 = 6, 110 = 6
Result:
Hexadecimal (Base-16)
The hexadecimal system uses sixteen symbols: 0-9 and A-F, where A=10, B=11, C=12, D=13, E=14, F=15. Each position represents a power of 16.
Converting Hexadecimal to Decimal: Multiply each digit/symbol by the corresponding power of 16.
Example: Convert to decimal. (Remember A = 10)
Converting Decimal to Hexadecimal: Repeatedly divide the decimal number by 16, recording remainders. Convert remainders > 9 to A-F. Read in reverse.
Example: Convert to hexadecimal.
- remainder 5
- remainder 10 (which is A)
- remainder 1
Reading bottom-up: 1A5. So, .
Relationship with Binary: Since , each hexadecimal digit corresponds directly to four binary digits.
| Hex | Binary | Hex | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | A | 1010 |
| 3 | 0011 | B | 1011 |
| 4 | 0100 | C | 1100 |
| 5 | 0101 | D | 1101 |
| 6 | 0110 | E | 1110 |
| 7 | 0111 | F | 1111 |
To convert binary to hex, group binary digits in fours from right to left. Convert hex to binary by replacing each hex digit with its four-digit binary equivalent.
Example: Convert to hex.
Group: 11 0101 1101 -> 0011 0101 1101
Convert: 0011 = 3, 0101 = 5, 1101 = D
Result:
Use Cases
- Hexadecimal:
- Color Codes: HTML/CSS colors (#FF0000 is red).
- Memory Addresses: Representing locations in computer memory.
- MAC Addresses: Network interface identifiers.
- Debugging: Viewing raw byte data.
- Octal:
- File Permissions: In Unix-like systems (e.g.,
chmod 755). - Older Systems: Was more common in early computing.
- File Permissions: In Unix-like systems (e.g.,
Code Examples
Python:
decimal_num = 250
# Decimal to Octal
octal_string = oct(decimal_num)
print(f"Decimal {decimal_num} is Octal: {octal_string}") # Output: Decimal 250 is Octal: 0o372
# Decimal to Hexadecimal
hex_string = hex(decimal_num)
print(f"Decimal {decimal_num} is Hex: {hex_string}") # Output: Decimal 250 is Hex: 0xfa
# Convert Octal string to Decimal
octal_num_string = "372"
decimal_from_octal = int(octal_num_string, 8) # Base 8
print(f"Octal {octal_num_string} is Decimal: {decimal_from_octal}") # Output: Octal 372 is Decimal: 250
# Convert Hex string to Decimal
hex_num_string = "1A5"
decimal_from_hex = int(hex_num_string, 16) # Base 16
print(f"Hex {hex_num_string} is Decimal: {decimal_from_hex}") # Output: Hex 1A5 is Decimal: 421
# Literals (0o for octal, 0x for hex)
print(0o372) # Output: 250
print(0x1A5) # Output: 421
Conclusion
Octal and hexadecimal number systems provide convenient shorthands for representing binary values, which are ubiquitous in computing. Understanding how to convert between decimal, binary, octal, and hexadecimal allows for a deeper comprehension of data representation, memory organization, and various programming concepts.
Try the tools
Keep reading
How to Make a Vision Board You'll Actually Look At
A step-by-step guide to making a vision board that changes what you do — not just how you feel — using what psychology research says about goals, obstacles and plans.
Read articleVision Board Ideas for 2027: 60 Captions and 3 Real Examples
Sixty vision board ideas you can steal, grouped by life area, plus three real example boards and the small changes that make a board worth looking at every day.
Read articleDORA Metrics Explained: The Four Keys to Delivery Performance
Deployment frequency, lead time, change failure rate, and time to restore — what the four DORA metrics measure, how to read them, and how to improve them without gaming the numbers.
Read article