Table of Contents
Bytes, not letters
Text on disk is bytes. A character set (charset) is the rule that maps bytes to characters. UTF-8 and GBK are two such rules. If you encode text with one rule and decode it with another, you get mojibake — the bytes are fine, the interpretation is wrong.
"中" in UTF-8 -> E4 B8 AD
"中" in GBK -> D6 D0
Decode E4 B8 AD as GBK and you get ä¸ — classic garbled Chinese. That is the whole bug.
UTF-8 vs GBK at a glance
| UTF-8 | GBK | |
|---|---|---|
| Coverage | Every Unicode character | Mostly Simplified Chinese + ASCII |
| Bytes per char | 1–4 (ASCII = 1) | 1 (ASCII) or 2 (Chinese) |
| ASCII compatible | Yes | Yes |
| Standard | Unicode | Chinese national (GB13000) |
UTF-8 is variable-length and ASCII-compatible, so English text costs one byte. GBK uses two bytes for Chinese and has no room for most non-Chinese scripts.
The usual causes
- A UTF-8 file opened as GBK (or vice versa) in an editor or terminal.
- An HTTP response missing
Content-Type: text/html; charset=utf-8, so the browser guesses. - A database connection using
gbkwhile the app writesutf8mb4. - Copying text between systems that disagree on the charset.
How to fix it
- Declare the charset everywhere:
<meta charset="utf-8">, the HTTP header, and the database/client encoding. - Standardize on UTF-8 for new work.
- Convert, don’t re-save: re-saving garbled text bakes in the error. Convert the bytes from the source charset to the target.
# convert a GBK file to UTF-8
iconv -f GBK -t UTF-8 input.txt > output.txt
Convert between charsets
Turn GBK/Big5/UTF-8 around without losing data using the Charset Convert tool on Toolbox.
Related tools
Next steps
- Before non-ASCII goes into a URL it is UTF-8 first, then percent-escaped — see URL Space: %20 or +.
- Binary data is wrapped in Base64 — see Base64 Images in CSS.
Frequently asked questions
What is mojibake?
Mojibake (乱码) is text shown with the wrong character set. The bytes are intact; the decoder just maps them to the wrong characters, producing gibberish like '䏿–‡'.
Is GBK still needed?
Legacy Chinese systems and some files use GBK/GB2312. New projects should standardize on UTF-8, but you still meet GBK when reading old data or talking to older services.
Why does it look fine in one editor but broken in another?
The editor guessed a different charset than the one used to write the file. Declaring the charset explicitly removes the guesswork.
Guide Info
- Category
- Explained
- Difficulty
- Beginner
- Reading Time
- 3 min
- Tags
- #utf-8 #gbk #encoding #charset
Next guide
SM4 vs AES: Parameter Differences Compared
Both are 128-bit block ciphers, but their key sizes, structure, and standards differ. Here is a side-by-side of the parameters that actually matter when you pick one.