Scrapeless Wiki

What Is Character Encoding? – Definition and How It Works

Informational P2 what is character encoding

Learn what character encoding is, how UTF-8 works, why mojibake happens, how encoding is declared on the web, and how to handle it correctly.

Character encoding is the mapping between characters and the bytes that represent them. Every text file and every HTTP response is bytes; the encoding is what turns those bytes back into readable text.

1. The Core Concepts

  • Character set β€” the collection of characters, each with a numeric code point.
  • Encoding β€” the rule for turning code points into bytes and back.
  • Unicode β€” the universal character set covering essentially every script.
  • UTF-8 β€” the dominant encoding of Unicode on the web.

Unicode says which characters exist; UTF-8 says how to store them.

2. Why UTF-8 Won

  • Backwards compatible with ASCII: the first 128 characters encode identically.
  • Variable width β€” one to four bytes per character, so common text stays compact.
  • Self-synchronising, so a corrupted byte does not destroy everything after it.
  • No byte order ambiguity, unlike wider fixed-width encodings.

3. Where Mojibake Comes From

Garbled text is bytes decoded with the wrong encoding β€” a UTF-8 sequence read as a single-byte encoding, most commonly.

"cafΓ©"  encoded UTF-8   β†’ 63 61 66 C3 A9
        decoded latin-1 β†’ "café"

The bytes are intact; only the interpretation is wrong. This is why the fix is almost always to decode correctly rather than to repair the text afterwards.

4. How Encoding Is Declared on the Web

Source Declaration
HTTP header Content-Type: text/html; charset=utf-8
HTML meta tag <meta charset="utf-8"> in the head
XML declaration <?xml version="1.0" encoding="utf-8"?>
Byte order mark Leading bytes signalling the encoding

The HTTP header takes precedence in browsers. When nothing is declared, clients guess β€” and guesses are frequently wrong on non-English content.

5. Handling It in Code

  • Decode explicitly where the encoding is known, rather than relying on library guessing.
  • Use the declared charset from the header, falling back to the document's own declaration.
  • Detect as a last resort, and record that detection was used.
  • Store as UTF-8 internally, converting at the boundary.
  • Compare normalised text β€” the same character can have multiple Unicode representations.

6. Normalisation

Unicode allows some characters to be composed in more than one way β€” a letter with an accent as a single code point, or as a base letter plus a combining mark. The two look identical and are not equal as strings.

Normalising before comparison, deduplication, or use as a key prevents entries that appear identical from behaving as different values.

7. Practical Traps

  • Assuming the platform default. It differs between systems and CI environments.
  • A byte order mark breaking strict parsers, particularly for JSON and CSV.
  • Length in bytes versus characters, which differ for anything non-ASCII.
  • Truncating by bytes, which can split a character and corrupt it.
  • Databases with a non-Unicode column encoding, silently replacing characters on write.

8. Summary

Character encoding maps characters to bytes; UTF-8 is the web's default and handles every script. Garbled text is almost always correct bytes decoded with the wrong rule.

Read the declared charset, decode explicitly, normalise before comparing, store UTF-8 internally, and never assume the platform default β€” most encoding bugs are one missing explicit decision.