Hexspeak in Python

Hexspeak is a novelty form of spelling that uses hexadecimal digits to represent words. In Python, we can convert decimal numbers to Hexspeak by first converting to hexadecimal, then replacing specific digits with letters.

Understanding Hexspeak Conversion

The conversion process involves these steps:

  • Convert decimal number to hexadecimal
  • Replace digit 0 with letter 'O' and digit 1 with letter 'I'
  • Keep hexadecimal letters A-F unchanged
  • If any other digits (2-9) exist, return "ERROR"

Valid Hexspeak characters are: {"A", "B", "C", "D", "E", "F", "I", "O"}

Implementation Using Built-in hex() Function

Here's a simpler approach using Python's built-in hex() function ?

def to_hexspeak(num):
    # Convert to hex and remove '0x' prefix
    hex_str = hex(int(num))[2:].upper()
    
    # Define the mapping for Hexspeak
    mapping = {'0': 'O', '1': 'I'}
    
    result = ""
    for char in hex_str:
        if char in mapping:
            result += mapping[char]
        elif char in "ABCDEF":
            result += char
        else:
            return "ERROR"
    
    return result

# Test examples
print(to_hexspeak("257"))   # 257 = 0x101
print(to_hexspeak("659724")) # 659724 = 0xA10C
print(to_hexspeak("1000"))   # Contains invalid digits
IOI
AIIOC
ERROR

Custom Hexadecimal Conversion Method

You can also implement the hexadecimal conversion manually ?

def convert_to_hex_digits(n):
    if n == 0:
        return [0]
    
    digits = []
    while n > 0:
        digits.append(n % 16)
        n //= 16
    return digits[::-1]  # Reverse to get correct order

def to_hexspeak_custom(num):
    hex_digits = convert_to_hex_digits(int(num))
    
    # Mapping from hex digit values to Hexspeak
    mapping = {
        10: "A", 11: "B", 12: "C", 13: "D", 
        14: "E", 15: "F", 0: "O", 1: "I"
    }
    
    result = ""
    for digit in hex_digits:
        if digit in mapping:
            result += mapping[digit]
        else:
            return "ERROR"
    
    return result

# Test examples
print(to_hexspeak_custom("257"))
print(to_hexspeak_custom("659724"))
print(to_hexspeak_custom("1000"))
IOI
AIIOC
ERROR

Example Breakdown

Let's trace through the conversion of 257 to Hexspeak ?

number = 257

# Step 1: Convert to hex
hex_value = hex(number)[2:].upper()
print(f"Decimal {number} = Hex {hex_value}")

# Step 2: Apply Hexspeak mapping
mapping = {'0': 'O', '1': 'I'}
hexspeak = ""

for char in hex_value:
    if char in mapping:
        hexspeak += mapping[char]
        print(f"'{char}' ? '{mapping[char]}'")
    elif char in "ABCDEF":
        hexspeak += char
        print(f"'{char}' ? '{char}' (unchanged)")

print(f"Final Hexspeak: {hexspeak}")
Decimal 257 = Hex 101
'1' ? 'I'
'0' ? 'O'
'1' ? 'I'
Final Hexspeak: IOI

Comparison

Method Pros Cons
Built-in hex() Simple, readable Relies on built-in function
Custom conversion Shows algorithm clearly More code, potential for bugs

Conclusion

Hexspeak conversion involves converting decimal to hexadecimal, then mapping specific digits to letters. Use the built-in hex() function for simplicity, or implement custom conversion to understand the underlying algorithm.

Updated on: 2026-03-25T08:10:03+05:30

979 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements