What Is Hex Encoding?
A complete guide to hexadecimal encoding: what it is, why it matters, and how developers use it every day.
Hexadecimal: The Language Between Binary and Human
Hexadecimal (hex) is a base-16 number system that uses sixteen symbols: the digits 0 through 9 and the letters A through F. Where decimal counts from 0 to 9 before needing a second digit, hex counts from 0 to F (fifteen) before rolling over to 10 (sixteen). This makes hex a compact way to represent binary data, because each hex digit maps to exactly 4 bits.
This property is why hex is ubiquitous in computing. A single byte (8 bits) is always exactly two hex digits. A 32-bit value is always exactly 8 hex digits. A 64-bit value is always exactly 16 hex digits. No rounding, no variable widths, no ambiguity. This predictability makes hex the preferred notation for any context where you need to reason about binary data at the byte level.
The Hex Alphabet
The sixteen hex digits and their decimal and binary equivalents:
| Hex | Decimal | Binary |
|---|---|---|
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 2 | 2 | 0010 |
| 3 | 3 | 0011 |
| 4 | 4 | 0100 |
| 5 | 5 | 0101 |
| 6 | 6 | 0110 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| 9 | 9 | 1001 |
| A | 10 | 1010 |
| B | 11 | 1011 |
| C | 12 | 1100 |
| D | 13 | 1101 |
| E | 14 | 1110 |
| F | 15 | 1111 |
The key insight is the last column: each hex digit is exactly one nibble (4 bits). To convert any hex value to binary, you replace each hex digit with its 4-bit equivalent. To go the other direction, group binary digits in sets of four from the right and look up each group. This mechanical conversion is why programmers can read hex fluently with a little practice.
How Hex Encoding Works
"Hex encoding" typically refers to the process of converting arbitrary data (text, binary files, network packets) into a string of hex characters. Each byte of input becomes two hex characters. The process is straightforward:
- Take a byte of input data (a value between 0 and 255)
- Split it into two nibbles: the high 4 bits and the low 4 bits
- Convert each nibble to its hex character
- Concatenate the results
For example, the ASCII character "A" has the byte value 65. In binary, that is 01000001. The high nibble is 0100 (4) and the low nibble is 0001 (1). So "A" encodes to 41 in hex. The string "Hello" becomes 48 65 6C 6C 6F.
// JavaScript: text to hex
function textToHex(text) {
return Array.from(new TextEncoder().encode(text))
.map(byte => byte.toString(16).padStart(2, '0'))
.join(' ');
}
textToHex('Hello'); // "48 65 6c 6c 6f"
// Hex back to text
function hexToText(hex) {
const bytes = hex.split(/\s+/)
.map(h => parseInt(h, 16));
return new TextDecoder().decode(new Uint8Array(bytes));
}
hexToText('48 65 6c 6c 6f'); // "Hello"Where You Encounter Hex
CSS Colors
The most familiar use of hex for many developers is CSS color codes. The format #RRGGBB uses two hex digits for each color channel (red, green, blue), giving 256 levels per channel and over 16 million possible colors. #FF6B35 means red = 255, green = 107, blue = 53, which is a warm orange. The shorthand #F63 is equivalent to #FF6633. Use our Color Bridge to decode any hex color.
Memory Addresses and Debugging
Debuggers, crash dumps, and system logs display memory addresses in hex. A 64-bit pointer like 0x7FFE4B3C8A10 is more readable than its decimal equivalent 140,729,033,845,264. Hex also aligns naturally with memory boundaries: a 4 KB page is 0x1000 bytes, and page-aligned addresses always end in 000.
Network Protocols and Packet Inspection
Tools like Wireshark, tcpdump, and hex editors display packet data in hex. When you inspect a TCP packet or HTTP request at the byte level, you see hex pairs representing each byte. MAC addresses (AA:BB:CC:DD:EE:FF) and IPv6 addresses (fe80::1) use hex notation directly.
File Signatures (Magic Bytes)
Every file format begins with a specific sequence of bytes called a magic number or file signature. PNG files start with 89 50 4E 47, PDFs start with 25 50 44 46 ("%PDF"), and ZIP files start with 50 4B 03 04 ("PK"). Forensic analysts and security researchers use these signatures to identify files regardless of their extension. See our Magic Bytes guide for the complete reference.
Character Encodings
When debugging encoding issues (mojibake, corrupted characters, BOM markers), hex is the diagnostic tool. Viewing the raw bytes in hex reveals whether a file is UTF-8, UTF-16, or Latin-1. The UTF-8 BOM is EF BB BF. A UTF-16 BOM is FF FE (little-endian) or FE FF (big-endian). Our Hex Encoder supports multiple encodings for exactly this purpose.
Hex vs Base64
Both hex and Base64 encode binary data as text, but they make different trade-offs. Hex is exactly 2x the size of the original data (each byte becomes two characters). Base64 is roughly 1.33x (each group of 3 bytes becomes 4 characters). Base64 is more space-efficient, which is why it is used for embedding binary in JSON, email (MIME), and data URIs.
Hex is preferred when readability matters more than compactness. Each byte is directly visible as two characters, and you can mentally convert between hex and binary. Base64 obscures the individual bytes, making it harder to reason about the underlying data. For debugging, forensics, and protocol analysis, hex is the standard.
Separators and Formatting
Hex strings can be formatted in many ways depending on the context:
- Space-separated:
48 65 6C 6C 6F(most readable for hex dumps) - No separator:
48656C6C6F(compact, used in APIs and hashes) - Colon-separated:
48:65:6C:6C:6F(MAC addresses, certificate fingerprints) - 0x-prefixed:
0x48 0x65 0x6C 0x6C 0x6F(C/Go-style byte literals)
Our converter lets you switch between these formats with one click. The choice of separator is purely cosmetic; the underlying byte values are identical.
Try It Yourself
Our Hex Encoder/Decoder converts between text and hex in real time with multiple separator formats and character encodings. The Hex Dump tab generates xxd-style output with clickable bytes. Drop any file into the File Viewer to inspect its bytes and detect its type from magic bytes.
Further Reading
- Hexadecimal — Wikipedia
History and mathematics of the base-16 number system.
- xxd Manual — Linux Man Pages
The classic Unix hex dump utility that inspired our Hex Dump view.
- MDN: TextEncoder / TextDecoder
Browser APIs for converting between text and byte arrays.
- RFC 3629 — UTF-8 Encoding
The UTF-8 standard that defines how Unicode code points map to the hex byte sequences you see in hex dumps.
- The Absolute Minimum Every Developer Must Know About Unicode — Joel Spolsky
Classic article explaining character encodings and why understanding hex byte representations matters.