> HEX (HEXADECIMAL) ENCODING
The idea in plain English: You know how we normally count in base-10 (0-9)? Computers count in base-2 (0-1, binary). But binary numbers get very long very fast — the number 255 in binary is 11111111 (eight digits!). Hexadecimal (hex for short) is base-16: it uses 0-9 plus A-F as digits, where A=10, B=11, C=12, D=13, E=14, F=15. One hex digit represents exactly 4 bits, and two hex digits represent one byte (8 bits). It's a compact way to show binary data.
Why this really exists: Hex is everywhere in programming. Web colors like
#FF5733 are hex (FF=red, 57=green, 33=blue). Memory addresses in debuggers and error
messages are shown as hex. When you look at raw binary data (a hex dump), it's displayed as hex.
Even this webpage uses hex color codes in its CSS.
▸ Concrete Example
The hex string "48656C6C6F" decodes to the word "Hello":
65 → decimal 101 → ASCII 'e'
6C → decimal 108 → ASCII 'l'
6C → decimal 108 → ASCII 'l'
6F → decimal 111 → ASCII 'o'
"48656C6C6F" → "Hello"
The hex digits come in pairs because each pair = one byte = one ASCII character. Just split the string into pairs, convert each pair to decimal, then to a character.
▸ How to Decode (Step by Step)
1. Split the hex string into pairs of characters: 48 65 6C 6C 6F
2. Convert each pair from base-16 to decimal: 48 = 4×16 + 8 = 72
3. Convert the decimal number to an ASCII character: 72 = 'H'
4. Join all characters → the decoded message!
plain = bytes.fromhex(ciphertext).decode('utf-8')
# Or manually:
chars = [chr(int(ciphertext[i:i+2], 16)) for i in range(0, len(ciphertext), 2)]
plain = ''.join(chars)
💡 Unlike Base64, hex doesn't use padding — every pair of characters is exactly one byte. If the hex string has an odd length, there's likely a typo.
▸ Hex Quick Reference
| Hex | Decimal | Binary | ASCII (if printable) |
|---|---|---|---|
| 00 | 0 | 00000000 | NUL (null) |
| 20 | 32 | 00100000 | Space |
| 30 | 48 | 00110000 | '0' |
| 41 | 65 | 01000001 | 'A' |
| 61 | 97 | 01100001 | 'a' |
| 7A | 122 | 01111010 | 'z' |
| FF | 255 | 11111111 | (not printable) |
▸ Real-World Applications
- Web colors: #FF5733 in CSS is hex for red=255, green=87, blue=51
- Memory addresses: Errors like "Segfault at 0x7FFF2A3B" use hex
- MAC addresses: Every network card has a hex ID like "00:1A:2B:3C:4D:5E"
- Hash displays: SHA-256 hashes are shown as 64 hex characters (e.g. "e3b0c442...")
- Hex editors: Tools like HxD let you view and edit raw file bytes in hex