Every character you type — from A to 中 to 😂 — is stored in a computer as a sequence of 0s and 1s. The bridge between human-readable text and machine binary is character encoding. This article explains how text is converted to binary, covering the foundational ASCII standard, the transition to Unicode, and the practical encoding schemes (UTF-8, UTF-16) that power the modern web. You'll walk away with a clear mental model, a worked example, and the knowledge to diagnose encoding-related bugs.

What Is Character Encoding?
A character encoding is a mapping between a set of characters (letters, digits, symbols) and their numeric representations. The computer stores numbers; the encoding tells it which number corresponds to which glyph. Without a consistent encoding, the same binary sequence can be interpreted as different characters on different systems — the root cause of mojibake (garbled text).
ASCII: The Foundation
ASCII (American Standard Code for Information Interchange), introduced in 1963, uses 7 bits to represent 128 characters: control codes (0–31), printable characters (32–126), and the delete character (127). In practice, it occupies 1 byte (8 bits), with the high bit often set to 0.
Key ASCII values to remember:
| Character | Decimal | Binary |
|---|---|---|
| Space | 32 | 0010 0000 |
0 |
48 | 0011 0000 |
A |
65 | 0100 0001 |
a |
97 | 0110 0001 |
Notice that uppercase and lowercase letters differ by exactly 32 ('a' - 'A' = 32). This regularity makes case conversion trivial in code.
Beyond ASCII: Unicode and Encoding Schemes
ASCII only covers English. To represent characters from other languages (e.g., Chinese, Arabic, emoji), we need a much larger character set. Unicode assigns a unique code point (e.g., U+4E2D for 中) to every character, but does not specify how to store those code points in bytes. That's where encoding schemes like UTF-8 and UTF-16 come in.
UTF-8: Variable-Length, ASCII-Compatible
UTF-8 is the dominant encoding on the web. It uses 1 to 4 bytes per character:
- ASCII characters (U+0000–U+007F): 1 byte (identical to ASCII)
- Latin, Greek, etc. (U+0080–U+07FF): 2 bytes
- CJK characters (U+0800–U+FFFF): 3 bytes
- Supplementary characters (U+10000–U+10FFFF): 4 bytes
A key design feature: the first byte of a multi-byte sequence starts with 110, 1110, or 11110 (indicating 2, 3, or 4 total bytes), while continuation bytes always start with 10. This makes UTF-8 self-synchronizing — you can always find character boundaries even if you start in the middle of a stream.
UTF-16: Fixed-Width for BMP, Variable Beyond
UTF-16 uses 2 bytes for characters in the Basic Multilingual Plane (BMP, U+0000–U+FFFF) and 4 bytes (surrogate pairs) for others. It is commonly used internally by Java, .NET, and Windows. However, it is not ASCII-compatible — ASCII characters become 2 bytes with a leading zero byte, doubling storage for English text.
How Text Becomes Binary: A Worked Example
Let's convert the string "Hi 中" to binary step by step.
Identify the Unicode code points:
H→ U+0048i→ U+0069(space) → U+0020中→ U+4E2D
Encode each code point in UTF-8:
- U+0048 (0x48) is in the ASCII range → 1 byte:
0100 1000 - U+0069 (0x69) → 1 byte:
0110 1001 - U+0020 (0x20) → 1 byte:
0010 0000 - U+4E2D (0x4E2D) is in the 3-byte range → 3 bytes:
- Binary of code point:
0100 1110 0010 1101(16 bits) - UTF-8 3-byte template:
1110xxxx 10xxxxxx 10xxxxxx - Fill:
1110 0100 1011 1000 1010 1101→ hex:E4 B8 AD
- Binary of code point:
- U+0048 (0x48) is in the ASCII range → 1 byte:
Concatenate all bytes:
0100 1000 0110 1001 0010 0000 1110 0100 1011 1000 1010 1101
You can verify this result with our Text to Binary Converter tool.
Common Pitfalls
- Mixing encoding and code page: A file saved as GBK but read as UTF-8 produces mojibake (e.g., "锟斤拷"). Always know your source encoding.
- Assuming ASCII is enough: Non-English text will be corrupted if you truncate to 7 bits or treat multi-byte sequences as single characters.
- Confusing Unicode with UTF-8: Unicode defines code points; UTF-8 is one way to store them. They are not interchangeable terms.
- Byte order marks (BOM): UTF-16 files often start with a BOM (
U+FEFF) to indicate endianness. Some tools choke on BOM in UTF-8. - char type signedness: In C/C++,
charmay be signed or unsigned by default. Cast tounsigned charbefore comparing or arithmetic to avoid sign extension issues.
When to Use Which Encoding
| Scenario | Recommended Encoding | Reason |
|---|---|---|
| Web pages, APIs, JSON | UTF-8 | Universal, ASCII-compatible, self-synchronizing |
| Internal Windows apps | UTF-16 | Native to Windows API, fast for BMP characters |
| Legacy Chinese systems | GBK | Compact for Chinese text, but avoid for new projects |
| Embedded systems | UTF-8 | Space-efficient for mostly-ASCII logs |
| Cross-platform files | UTF-8 without BOM | Most compatible across OSes |
FAQ
What is the difference between ASCII and Unicode?
ASCII is a 7-bit encoding covering 128 English characters. Unicode is a much larger character set (over 140,000 characters) that assigns a unique code point to every character from all writing systems. ASCII is a subset of Unicode (U+0000–U+007F).
Why does UTF-8 use variable length?
Variable length allows UTF-8 to remain backward-compatible with ASCII (1 byte per character) while supporting the full Unicode range. A fixed-width encoding like UTF-32 would waste space for common ASCII text.
How do I detect the encoding of a file?
There is no foolproof method, but you can look for a BOM, analyze byte patterns (e.g., UTF-8 validation), or use heuristic libraries like chardet (Python) or enca. For reliable results, always store metadata about encoding alongside the file.
What causes "锟斤拷" in text?
This is a classic mojibake artifact. When a file encoded in GBK (or GB2312) is decoded as UTF-8, certain byte sequences produce replacement characters that, when re-encoded, yield the "锟斤拷" pattern. It's a chain of encoding mismatches.
Is UTF-8 always the best choice?
For most modern applications — especially web, cloud, and cross-platform — yes. UTF-8 is compact for ASCII, handles all Unicode characters, and is the default for HTML, JSON, and many programming languages. Exceptions include systems with heavy CJK text where GBK may save space, or environments where UTF-16 is the native string format (e.g., Java, .NET internals).