Text Encoding and Unicode
Text encoding is the mapping between characters and the bytes computers store. Unicode is the universal character set that covers modern and historical scripts, and UTF-8 is its dominant byte serialization. This topic covers how text moves from abstract characters to bytes and back, why encodings break, and how to handle text correctly across systems.
itComputer fundamentals | OpenSkills.info
Intro
Text Encoding and Unicode
Text looks natural on a screen, but a computer stores only bytes. A character encoding is the mapping that turns characters into bytes and bytes back into characters. Without an agreed encoding, the same bytes render as different text on different systems.
The problem is older than the web. In the 1980s, a byte held 256 patterns and each vendor or language assigned those patterns to a different set of characters. A document written on one machine became mojibake on another. Mixing languages in one document was hard because each encoding covered one script family. Software shipped with a patchwork of code pages and guessed which one applied.
Unicode solved that. It is a single coded character set that assigns a unique number, called a code point, to every character it covers. The repertoire covers modern and historical scripts, mathematical symbols, punctuation, and emoji. Unicode 17.0 assigns 159,801 characters across 172 scripts. Text encoded as Unicode can travel between systems without losing characters, and a string has one meaning everywhere.
Unicode separates three ideas that older encodings collapsed: the character repertoire, the code points that number it, and the encoding forms that serialize code points to code units and bytes. Keeping those layers distinct is the key to working with text correctly.
Characters, code points, and bytes
Three terms anchor everything else.
A character is the smallest unit of written language with semantic value. A letter, a digit, a punctuation mark, and a symbol are characters. Unicode draws a strict line between a character and the glyph that renders it. The letter A is one character; many typefaces draw it with different shapes. Those shapes are glyphs, and Unicode does not encode them.
A code point is the number Unicode assigns to a character. Code points range from U+0000 to U+10FFFF, a space of 1,114,112 values. The notation U+0041 means the code point at hexadecimal 41, which is the character LATIN CAPITAL LETTER A. Code points group into 17 planes of 65,536 values each. Plane 0 is the Basic Multilingual Plane and holds most modern scripts. The other planes hold supplementary characters such as historic scripts, musical symbols, and supplementary CJK ideographs.
A code unit is the minimal piece an encoding form uses. UTF-8 uses 8-bit code units, UTF-16 uses 16-bit code units, and UTF-32 uses 32-bit code units. The encoding form maps each code point to a sequence of code units; the encoding scheme then serializes those units as bytes.
character -> code point -> code units -> bytes
A U+0041 41 41
₣ U+20A3 E2 82 A3 E2 82 A3
❤ U+2764 E2 9D A4 E2 9D A4
𠮷 U+20BB7 D842 DFB7 F0 A0 AE B7
Continue the course
This section is part of the paid course.
See pricing to subscribe, or log in if you already have access.
Sources
- https://www.unicode.org/versions/Unicode17.0.0/core-spec/chapter-2/
Supports
- Unicode separates characters, code points, encoding forms, and encoding schemes
- A character is the smallest unit of written language with semantic value; glyphs are visual shapes chosen by a font
- Code points range from U+0000 to U+10FFFF and group into 17 planes of 65,536 each
- The Basic Multilingual Plane holds most modern scripts; supplementary characters sit above U+FFFF
- UTF-8 encodes each scalar value as one to four bytes; UTF-16 uses 16-bit code units; UTF-32 uses one 32-bit unit per scalar value
- The surrogate range U+D800 to U+DFFF is used only by UTF-16 and never maps to a character
- Combining marks attach to a base character, so one user-perceived character can contain multiple code points
- Canonically equivalent sequences render the same but differ in bytes; normalization resolves them
- Characters, once assigned, cannot be reassigned; character names are stable
- https://www.unicode.org/versions/Unicode17.0.0/core-spec/chapter-3/
Supports
- A conformant process must detect ill-formed byte sequences and must not interpret them as characters
- The Unicode Standard defines encoding forms, encoding schemes, and conformance requirements
- https://www.rfc-editor.org/info/rfc3629/
Supports
- UTF-8 encodes each Unicode scalar value as one to four bytes
- The bit patterns distinguish leading bytes from continuation bytes; continuation bytes start with 10
- UTF-8 is self-synchronizing: a parser can find the next character boundary from any byte
- The shortest encoding is required; overlong encodings are invalid
- Surrogate code points are not valid in UTF-8
- https://www.unicode.org/reports/tr15/
Supports
- NFC, NFD, NFKC, and NFKD are the four normalization forms
- NFC composes canonically equivalent sequences; NFD decomposes them
- NFKC and NFKD apply compatibility decomposition, which is lossy
- Compatibility decomposition folds superscripts, subscripts, and fullwidth forms into their base
- https://www.unicode.org/reports/tr29/
Supports
- A grapheme cluster is a base character plus its combining marks treated as a unit
- Grapheme cluster boundaries govern cursor movement, selection, and deletion
- Extended grapheme clusters are the recommended boundary model
- https://www.unicode.org/reports/tr10/
Supports
- The Unicode Collation Algorithm supplies a default multilingual string ordering
- Code-point order is not a culturally expected sort order
- The algorithm provides a tailoring mechanism for locale-specific ordering; CLDR supplies locale data
- https://www.unicode.org/reports/tr39/
Supports
- Confusable detection identifies visually similar characters with different code points
- Restriction profiles limit which characters are accepted in secure identifiers
- The General Security Profile is a default recommendation for identifier safety
- https://www.unicode.org/reports/tr31/
Supports
- UAX
- Identifier modification and restriction profiles work alongside UTS
- https://www.w3.org/International/articles/definitions-characters/
Supports
- A character set, a coded character set, and a character encoding are distinct concepts
- Unicode code points range from 0x0000 to 0x10FFFF
- UTF-8 uses one byte for ASCII, two for several alphabetic blocks, three for the rest of the BMP, four for supplementary characters
- UTF-16 uses two bytes for BMP characters and four for supplementary characters
- UTF-32 uses four bytes for all characters
- A user-perceived character can be a sequence of code points; grapheme clusters approximate that unit
- https://www.w3.org/International/questions/qa-choosing-encodings.en
Supports
- UTF-8 is the recommended encoding for web content
- The charset declaration must appear early in an HTML document
- HTTP Content-Type and HTML meta tags declare the encoding at the boundary
- https://www.unicode.org/glossary/
Supports
- The Consortium's definitions for code point, scalar value, grapheme cluster, canonical equivalence, and related terms
- https://www.unicode.org/versions/Unicode17.0.0/
Supports
- Unicode 17.0 assigns 159,801 characters across 172 scripts
- The standard consists of the core specification, code charts, Unicode Standard Annexes, and the Unicode Character Database
