Text Encoding Explained: Base64, URL Encoding, Binary, and More
Understand text encoding formats including Base64, URL encoding, binary, hex, and ASCII. Learn when to use each encoding and how they work.
Text encoding is the process of converting text into a specific format for transmission or storage. Different encoding formats serve different purposes — from safely transmitting data in URLs to storing binary data as text. Understanding encoding is essential for developers, webmasters, and anyone working with digital text.
What Is Text Encoding?
Computers store data as numbers. Text encoding defines how characters (letters, numbers, symbols) map to numeric values. The most fundamental encoding is ASCII, which assigns a number (0–127) to each character. For example, 'A' = 65, 'B' = 66, 'a' = 97.
Modern systems use Unicode (UTF-8), which supports over 1 million characters including emoji, Chinese characters, Arabic script, and more. UTF-8 is backward-compatible with ASCII.
Base64 Encoding
Base64 encoding converts binary data into a text string using 64 characters (A-Z, a-z, 0-9, +, /). It's commonly used to:
- Embed images in CSS or HTML (data URIs)
- Encode email attachments (MIME)
- Transmit binary data over text protocols
- Store complex data in JSON or XML
Base64 increases data size by about 33%. A 3-byte binary sequence becomes 4 text characters. While this overhead is significant, Base64 ensures data survives transmission through systems that only handle text.
URL Encoding
URL encoding (also called percent-encoding) converts special characters into a format safe for URLs. Characters like spaces, ampersands, and non-ASCII characters are replaced with % followed by two hex digits.
For example:
- Space becomes %20
- & becomes %26
- + becomes %2B
- Chinese characters become sequences like %E4%BD%A0%E5%A5%BD
URL encoding is essential for:
- Building query strings with special characters
- Ensuring URLs work across all browsers and servers
- Preventing injection attacks through URL parameters
Binary Encoding
Binary encoding represents each character as an 8-bit binary number (a sequence of 0s and 1s). For example, 'H' = 01001000, 'e' = 01100101.
Binary is the fundamental language of computers. Understanding binary encoding helps with:
- Debugging low-level data issues
- Understanding how computers store text
- Working with network protocols
- Learning computer science fundamentals
Hexadecimal Encoding
Hexadecimal (hex) encoding represents each character as a two-digit hex number. For example, 'H' = 48, 'e' = 65, 'l' = 6C.
Hex is more compact than binary (2 digits vs 8) and is widely used in:
- Color codes in CSS (#FF5733)
- Memory addresses in debugging
- Character encoding tables
- Cryptographic hash values
ASCII Codes
ASCII codes are the numeric values assigned to characters in the ASCII table. The ASCII table defines 128 characters (0–127), including:
- 0–31: Control characters (newline, tab, etc.)
- 32–126: Printable characters (letters, numbers, punctuation)
- 65–90: Uppercase letters (A–Z)
- 97–122: Lowercase letters (a–z)
- 48–57: Digits (0–9)
Comparison of Encoding Formats
| Format | Output Example | Size Increase | Common Use |
|---|---|---|---|
| Base64 | SGVsbG8= | +33% | Email, data URIs |
| URL Encoding | Hello%20World | Variable | URLs, query strings |
| Binary | 01001000 01100101 | 8x | Low-level computing |
| Hexadecimal | 48 65 6C 6C 6F | 2x | Colors, debugging |
| ASCII | 72 101 108 108 111 | 3x | Character mapping |
