Text Kodierung Referenz
Referenz für Zeichen encodings: UTF-8, UTF-16, UTF-32, und Konvertierung zwischen them.
UTF Encoding Forms
UTF-8 uses 1-4 bytes per code point: ASCII (1 byte), Latin (2), CJK (3), emoji/auxiliary (4). UTF-16 uses 2 bytes for BMP and 4 (surrogate pairs) for supplementary planes. UTF-32 uses 4 bytes for every code point (simple but space-inefficient). UTF-8 is the dominant web encoding (98%+ of pages). UTF-8 is ASCII-compatible and variable-length. UTF-16 is used internally by Java, JavaScript, and Windows APIs. BOM (U+FEFF) indicates byte order but is optional and sometimes problematic.
- UTF-8
- 1-4 bytes, ASCII compat
- UTF-16
- 2 or 4 bytes
- BOM
- Byte order mark
web standard Java/JS internal optional Conversion and Pitfalls
Converting between encodings requires correct handling of invalid sequences and unmappable characters. Replacement character (U+FFFD) substitutes for invalid bytes. Surrogate halves in UTF-16 are invalid alone and must be handled. JavaScript strings are UTF-16; use TextEncoder/TextDecoder for UTF-8 conversion. Length differs by encoding: str.length is UTF-16 code units, not code points or bytes. Counting code points requires iterating with for...of or [...str]. Byte length of UTF-8 requires new TextEncoder().encode(str).length.
- U+FFFD
- Replacement char
- JS strings
- UTF-16 internally
- byte length
- TextEncoder
invalid bytes code units UTF-8 bytes