Unidad de código
La unidad mínima de codificación: un byte de 8 bits en UTF-8, una palabra de 16 bits en UTF-16, una palabra de 32 bits en UTF-32. Un solo carácter puede requerir múltiples unidades de código.
What is a Code Unit?
A code unit is the minimal bit combination used in a Unicode encoding. Different encodings use different code unit sizes:
- UTF-8: 8-bit code units (bytes)
- UTF-16: 16-bit code units (2-byte words)
- UTF-32: 32-bit code units (4-byte words)
A code unit is not the same as a code point. Code points are abstract Unicode values (U+0000–U+10FFFF); code units are the concrete building blocks that encodings use to represent those values. One code point may require one or more code units depending on the encoding and the code point's value.
Code Units by Encoding
UTF-8 (8-bit code units)
UTF-8 uses 1 to 4 bytes per code point, following a variable-length scheme:
| Code point range | Code units | Byte pattern |
|---|---|---|
| U+0000–U+007F | 1 | 0xxxxxxx |
| U+0080–U+07FF | 2 | 110xxxxx 10xxxxxx |
| U+0800–U+FFFF | 3 | 1110xxxx 10xxxxxx 10xxxxxx |
| U+10000–U+10FFFF | 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
'A' (U+0041) → 1 code unit: 0x41
'é' (U+00E9) → 2 code units: 0xC3 0xA9
'中' (U+4E2D) → 3 code units: 0xE4 0xB8 0xAD
'😀' (U+1F600) → 4 code units: 0xF0 0x9F 0x98 0x80
UTF-16 (16-bit code units)
BMP characters (U+0000–U+FFFF) use 1 code unit. Supplementary characters use 2 code units (a surrogate pair):
'A' (U+0041) → 1 code unit: 0x0041
'中' (U+4E2D) → 1 code unit: 0x4E2D
'😀' (U+1F600) → 2 code units: 0xD83D 0xDE00 (surrogate pair)
UTF-32 (32-bit code units)
Every code point uses exactly 1 code unit — UTF-32 is the only fixed-width Unicode encoding:
'A' (U+0041) → 0x00000041
'中' (U+4E2D) → 0x00004E2D
'😀' (U+1F600) → 0x0001F600
Why Code Units Matter in Programming
Many programming languages expose string length in terms of code units, not code points or grapheme clusters:
s = "😀"
len(s) # Python 3: 1 — counts code points (Unicode scalars)
len(s.encode("utf-8")) # 4 — UTF-8 code units (bytes)
"😀".length // 2 — JavaScript counts UTF-16 code units
[..."😀"].length // 1 — spread iterator counts code points
String s = "😀";
s.length() // 2 — Java String.length() counts UTF-16 code units
s.codePointCount(0, s.length()) // 1 — code point count
This is a common source of bugs: naive string slicing by index in JavaScript or Java can split a surrogate pair, producing invalid text.
Common Pitfalls
Confusing code units with bytes: A UTF-16 code unit is 2 bytes, not 1. A string of length
n in a Java String occupies at least 2n bytes.
Assuming 1 code unit = 1 character: A single user-visible character (grapheme cluster) may require multiple code points, each potentially requiring multiple code units.
Slicing strings at byte offsets: UTF-8 continuation bytes begin with 10xxxxxx. Slicing
between them produces invalid UTF-8 sequences.
Quick Facts
| Property | Value |
|---|---|
| UTF-8 code unit size | 8 bits (1 byte) |
| UTF-16 code unit size | 16 bits (2 bytes) |
| UTF-32 code unit size | 32 bits (4 bytes) |
| UTF-8 code units per BMP char | 1–3 |
| UTF-16 code units per BMP char | 1 |
| UTF-16 code units per supplementary char | 2 (surrogate pair) |
| Only fixed-width encoding | UTF-32 |
| Language using UTF-16 internally | Java, JavaScript (V8), C# (.NET), Windows |
Términos relacionados
Más en Estándar Unicode
Plano 0 (U+0000–U+FFFF), que contiene los caracteres más utilizados: Latin, Griego, Cirílico, …
Unidad de información usada para organizar, controlar o representar datos textuales — …
Punto de código al que se le ha asignado un carácter en …
Chino, Japonés y Coreano — el término colectivo para el bloque de …
Organización sin fines de lucro que desarrolla y mantiene el Estándar Unicode. …
El rango completo de posibles puntos de código Unicode: U+0000 a U+10FFFF …
The process of mapping Chinese, Japanese, and Korean ideographs that share a …
The individual consonant and vowel components (jamo) of the Korean Hangul writing …
Estándar internacional (ISO/IEC 10646) sincronizado con Unicode, que define el mismo repertorio …
Puntos de código reservados permanentemente para uso interno (66 en total): U+FDD0–U+FDEF …