Character Encodings Shouldn’t be Ignored

UNICODE
TECH
LinkedIn Post

I used to take character encodings for granted until I hit my first ’ at Oracle. It happened because one of our data sources used ISO-8859-1 while we use UTF-8 like 99% of the world. So, I had to learn about character encodings in detail.

Keep reading to learn a bit about Unicode from my point-of-view.

Unicode assigns code points to characters, defines ligatures, etc. For example,

  • "A" → U+0041
  • "👍" → U+1F44D
  • "👍🏻" → U+1F44D + U+1F3FB (an example of two code points combining to form a single grapheme)

U+0041, U+1F44D, and U+1F3FB are code points.

Since computer only understand 0s and 1s, the code points are converted to bytes using Unicode Transformation Format (UTF). There are multiple UTFs e.g. UTF-8, UTF-16, and UTF-32.

UTF-32 is the easiest one because it represents each code point as a fixed 32-bit value. For example:

  • "A" → U+0041 → 00 00 00 41 (UTF-32, big-endian)
  • "👍" → U+1F44D → 00 01 F4 4D (UTF-32, big-endian)

UTF-8 is the most popular because it's ASCII-compatible and compact. It is a variable-length encoding where code points are encoded using 1 to 4 bytes. For example:

  • "A" → U+0041 → 41 (UTF-8)
  • "👍" → U+1F44D → F0 9F 91 8D (UTF-8)
UTF-8 vs UTF-32 bytes and decoding outcomes for A, thumbs up, and thumbs up with light skin tone
UTF-8 vs UTF-32 — same characters, different byte layouts and decode outcomes

When you mix up encoding, it MAY cause mojibake. And it often happens when the data travels from one system to another because both systems might have different system encodings. I found an example in the Google News app.

Google News app screenshot showing mojibake text ’ caused by an encoding mismatch
Real-world mojibake in the Google News app

The screenshot above shows the user-facing issue. The diagram below explains what happened at the byte level.

Right quote U+2019 encoded as UTF-8 bytes E2 80 99, then decoded as Windows-1252 into ’
How U+2019 (’) becomes ’ when UTF-8 bytes are decoded as Windows-1252