Character encoding (ASCII, Unicode, UTF-8)
From A = 65 to an emoji in four bytes: how text becomes numbers, how UTF-8 packs those numbers into bytes, and why “café” sometimes turns into “café”.
In 30 seconds
Computers store numbers, so text needs a table that maps characters to numbers. ASCII was an early one: 7 bits, 128 characters, enough for English.
Unicode gives every character in every script its own number, a code point. UTF-8 is the usual way to store those numbers: one byte for ASCII characters, up to four for the rest.
Bytes carry no label saying which table they use. Read them with the wrong one and you get garbled text. And since characters differ in size, a string has more than one “length”.
Character encoding (ASCII, Unicode, UTF-8), step by step
Simplified: one visible character can also be several code points, such as e plus a combining accent, or many emoji, so even counting code points can mislead. Latin-1 here is ISO 8859-1.
- Step 1: Text is stored as numbers. A computer stores only numbers, written in bits. To keep text, everyone has to agree on a table that gives each character a number: A is 65, which is stored as the bits 01000001. A word is then just a row of such numbers. That table is called a character encoding.
- Step 2: ASCII: seven bits. The table that became the common base is ASCII. It uses 7 bits, which gives 128 numbers: 33 control codes such as tab and new line, and 95 printable characters covering the English letters, the digits and common punctuation. There is no room for an accented letter, let alone Greek or Chinese.
- Step 3: Unicode: a number for all. Unicode is one table for every writing system. It gives each character a number called a code point, written U+ followed by the number in hexadecimal. The first 128 code points are exactly ASCII. A code point is still only a number: Unicode alone does not say how to store it as bytes.
- Step 4: UTF-8: one to four bytes. UTF-8 is the usual way to turn code points into bytes. Small code points take one byte and larger ones take two, three or four. The leading bits of the first byte say how many bytes belong together, and every following byte starts with 10. An ASCII character is a single byte with the same value as before.
- Step 5: Encode é, bit by bit. Take é, code point U+00E9, which is 233. That is above 127, the most one UTF-8 byte can carry, so the two-byte pattern applies, with room for 11 bits. Write 233 as 11 bits, put the first five after the marker 110 and the last six after the marker 10. The two bytes that result are C3 and A9 in hexadecimal.
- Step 6: Wrong table, garbled text. Bytes do not say which encoding they are in. The five bytes of “café” read as UTF-8 give back four characters. A program that assumes Latin-1, an older table with one byte per character, turns C3 and A9 into two separate characters. Nothing is damaged: reading the same bytes as UTF-8 puts it right.
- Step 7: How long is a string?. Because characters take different numbers of bytes, “length” has several answers. “café” is four characters but five bytes in UTF-8. The emoji is one character and one code point, yet JavaScript reports length 2, because it counts 16-bit units of another encoding, UTF-16. Cutting text by such a count can split a character.
Try it yourself
Learn this first
Learn next
- EndiannessWhich byte of a number comes first in memory, in files and on the network. Beginner
- Regular expressionsPatterns for matching text, and how the engine backtracks its way to a match. Intermediate
- Run-length encodingSqueezing repeated values into a count and a value, and when that backfires. Beginner
Spotted a mistake on this page?