Beyond Base 10: Converting Numbers to Hexadecimal and Octal

1/31/2025

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 3728372_8 to decimal.

3728=(3×82)+(7×81)+(2×80)372_8 = (3 \times 8^2) + (7 \times 8^1) + (2 \times 8^0) 3728=(3×64)+(7×8)+(2×1)372_8 = (3 \times 64) + (7 \times 8) + (2 \times 1) 3728=192+56+2=25010372_8 = 192 + 56 + 2 = 250*{10}

Converting Decimal to Octal: Repeatedly divide the decimal number by 8 and record the remainders. Read remainders in reverse order.

Example: Convert 25010250_{10} to octal.

  1. 250÷8=31250 \div 8 = 31 remainder 2
  2. 31÷8=331 \div 8 = 3 remainder 7
  3. 3÷8=03 \div 8 = 0 remainder 3

Reading bottom-up: 372. So, 25010=3728250_{10} = 372_8.

Relationship with Binary: Since 8=238 = 2^3, 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 10110110210110110_2 to octal. Group: 10 110 110 -> 010 110 110 Convert: 010 = 2, 110 = 6, 110 = 6 Result: 2668266_8

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 1A5161A5_{16} to decimal. (Remember A = 10)

1A516=(1×162)+(10×161)+(5×160)1A5*{16} = (1 \times 16^2) + (10 \times 16^1) + (5 \times 16^0) 1A516=(1×256)+(10×16)+(5×1)1A5*{16} = (1 \times 256) + (10 \times 16) + (5 \times 1) 1A516=256+160+5=421101A5*{16} = 256 + 160 + 5 = 421_{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 42110421_{10} to hexadecimal.

  1. 421÷16=26421 \div 16 = 26 remainder 5
  2. 26÷16=126 \div 16 = 1 remainder 10 (which is A)
  3. 1÷16=01 \div 16 = 0 remainder 1

Reading bottom-up: 1A5. So, 42110=1A516421*{10} = 1A5*{16}.

Relationship with Binary: Since 16=2416 = 2^4, 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 110101110121101011101_2 to hex. Group: 11 0101 1101 -> 0011 0101 1101 Convert: 0011 = 3, 0101 = 5, 1101 = D Result: 35D1635D_{16}

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.

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.

Explore More Blogs

3/28/2025

Introduction to Functions in Programming

Learn functions in programming: what they are, why they're used (DRY), how to de...

3/14/2025

Understanding Data Types in Programming (In...

Learn about fundamental data types in programming - integers, floating-point num...

2/7/2025

Understanding Compound Interest: Make Your ...

Learn the magic of compound interest – how earning interest on your interest can...