EMZETT.
Login

UTF-8 and character encoding

In short: A character encoding defines how letters are stored as bytes. UTF-8 is the common encoding today and can represent every Unicode character.

In more detail: Computers store only numbers. Unicode gives each character a number (code point); UTF-8 describes how that number is packed into 1 to 4 bytes. ASCII characters need only 1 byte, umlauts 2, many emojis 4.

In Depth

  • Backward compatible: Plain ASCII text is automatically valid UTF-8.
  • Mojibake: Wrong decoding produces garbage such as “ä” instead of “ä”.
  • Length: "ä".length counts characters, bytes or code units depending on the language. For emojis these numbers differ.
  • On the web: Set <meta charset="utf-8"> and the HTTP header Content-Type: text/html; charset=utf-8.
  • Databases: Columns with utf8mb4 (MySQL) store emojis completely.
  • BOM: An invisible byte order mark at the start of a file is unnecessary in UTF-8 and sometimes causes trouble.

See also: Endianness (byte order)