Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Why Unicode exists: from ASCII and code-page chaos to code points, UTF-8 and grapheme clusters, and why emoji break string length, slicing and databases.
Quick answer: Computers store only numbers, so text needs an agreed table that maps characters to numbers. For decades there were hundreds of incompatible tables, one per language or vendor, and text moved between them turned into garbage. Unicode is the single table that replaced them: it assigns one number, a code point, to every character in every writing system. UTF-8 is the usual way to store those numbers as bytes. Emoji break things because one visible symbol can be made of several code points, each taking several bytes, while a lot of code still assumes that one character equals one unit.
Also Read: Why Is Naming Things So Hard in Programming? - How To's
ASCII, standardised in the 1960s, used 7 bits for 128 characters: the English letters, digits, punctuation and some control codes. A is 65. It was enough for English and nothing else.
A byte has 8 bits, so 128 values were spare. Everyone filled them differently: one table for Western European accents, another for Cyrillic, another for Greek, and many more. Languages with thousands of characters, such as Chinese, Japanese and Korean, needed multi-byte schemes of their own.
The consequences:
Open text with the wrong table and you get mojibake: "café" turning into "café".
Unicode, first published in 1991, is one universal table. Each character gets a code point, written U+ followed by hexadecimal digits.
| Character | Code point |
|---|---|
| A | U+0041 |
| é | U+00E9 |
| Ж | U+0416 |
| 中 | U+4E2D |
| 😀 | U+1F600 |
The space runs from U+0000 to U+10FFFF, room for over a million code points, of which more than 150,000 are assigned. The first 128 match ASCII exactly.
Unicode says which number a character has. It does not say how to write that number as bytes. That is the job of an encoding.
| Encoding | Size per code point | Notes |
|---|---|---|
| UTF-32 | Always 4 bytes | Simple, wasteful |
| UTF-16 | 2 or 4 bytes | Used inside JavaScript, Java, .NET and Windows |
| UTF-8 | 1 to 4 bytes | The standard for files and the web |
Early Unicode assumed 65,536 characters would be enough, so 16 bits per character seemed fine. It was not enough. UTF-16 handles the overflow with surrogate pairs: two 16-bit units that together represent one code point. Most emoji need them.
UTF-8, designed by Ken Thompson and Rob Pike in 1992 and specified in RFC 3629, is the one that won.
| Code point range | Bytes | Example |
|---|---|---|
| U+0000 to U+007F | 1 | A |
| U+0080 to U+07FF | 2 | é |
| U+0800 to U+FFFF | 3 | 中 |
| U+10000 to U+10FFFF | 4 | 😀 |
Its advantages:
The large majority of web pages now use it.
| Text | What you see | Code points | UTF-16 units | UTF-8 bytes |
|---|---|---|---|---|
A | 1 | 1 | 1 | 1 |
é | 1 | 1 | 1 | 2 |
😀 | 1 | 1 | 2 | 4 |
👍🏽 | 1 | 2 | 4 | 8 |
👨👩👧 | 1 | 5 | 8 | 18 |
"😀".length // 2: JavaScript counts UTF-16 units
[..."😀"].length // 1: code points
"👨👩👧".length // 8
len("😀") # 1: Python counts code points
len("😀".encode("utf-8")) # 4 bytes
len("👨👩👧") # 5
What a reader sees as one character is called a grapheme cluster. Emoji are often built by combination:
�.utf8 character set stores at most 3 bytes per character, so it cannot hold emoji. The fix is utf8mb4. See SQL vs NoSQL for more on choosing a database.. matches half an emoji unless Unicode mode is on. See how regular expressions work.é can be one code point (U+00E9) or two: e followed by a combining accent (U+0301). They look identical and compare as different.
"é" === "é" // false
"é" === "é".normalize("NFC") // true
Normalisation converts text to one consistent form (NFC or NFD). Do it before comparing, searching or storing identifiers.
i is İ, not I. German ß upper-cases to SS.a and Cyrillic а are different code points that look the same, which enables spoofed domain names and usernames.<meta charset=utf-8> and Content-Type: text/html; charset=utf-8.Intl.Segmenter; elsewhere, an ICU-based library.utf8mb4.Like floating-point numbers and time zones, text looks simple until real-world data arrives. Language models face a related problem when they split text into tokens; see how tokenization works.
Unicode is the table that assigns a number to each character. UTF-8 is one way of writing those numbers as bytes.
JavaScript strings are sequences of UTF-16 code units, and most emoji need two.
Garbled text produced when bytes written in one encoding are read as another.
UTF-8 for storage and exchange. UTF-16 mostly appears as the internal string format of certain languages and platforms.
Unicode exists because the world's writing could not be squeezed into incompatible 256-character tables. It solved that problem thoroughly, and exposed another: a "character" is not a simple unit. Use UTF-8 throughout, be clear about whether you are counting bytes, code points or visible characters, and let a well-tested library handle the rest.