{
  "id": "utf-sixteen",
  "title": "UTF-16",
  "lang": "en",
  "source_lang": "en",
  "summary": "UTF-16 is a Unicode encoding that represents each Unicode scalar value using one or two 16-bit code units, with byte order specified separately for storage or transmission.",
  "category": "computer-science",
  "subcategory": "software",
  "type": "concept",
  "aliases": [
    "Unicode Transformation Format—16-bit",
    "16-bit Unicode Transformation Format"
  ],
  "generated_at": "2026-11-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/utf-sixteen/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/utf-sixteen/",
      "json": "https://aiwiki.page/zh/utf-sixteen.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "UTF-16 is a variable-length character encoding for Unicode that represents each Unicode scalar value as either one or two 16-bit code units. Most characters in the Basic Multilingual Plane use one code unit; characters outside that plane use two, forming a surrogate pair. UTF-16 can represent every Unicode scalar value, including characters from historic scripts, musical symbols, and emoji. It is widely used in software interfaces and string-processing environments, particularly those associated with Microsoft Windows, Java, and JavaScript."
    },
    {
      "heading": "Encoding model",
      "text": "Unicode assigns numerical values called code points to characters and other elements of text. Code points range from U+0000 to U+10FFFF. The interval U+D800–U+DFFF is reserved for surrogate code points; these are not independently encoded characters. A Unicode scalar value is any Unicode code point outside that reserved interval.\n\nUTF-16 maps scalar values to sequences of 16-bit code units:\n\nUnicode range | UTF-16 representation\nU+0000–U+D7FF | One code unit equal to the code point\nU+D800–U+DFFF | Reserved for surrogates; not valid as independent scalar values\nU+E000–U+FFFF | One code unit equal to the code point\nU+10000–U+10FFFF | Two code units forming a surrogate pair\n\nA code unit contains 16 bits, or two eight-bit bytes. Thus, the UTF-16 encoding of a scalar value occupies either two or four bytes when serialized, excluding any byte-order mark or surrounding protocol information.\n\nThe Basic Multilingual Plane (BMP), U+0000–U+FFFF, contains many commonly used writing systems, punctuation marks, and symbols. The remaining 16 planes are supplementary planes. These include additional Chinese characters, historic scripts, and many emoji.\n\nUTF-16 is therefore not a fixed-width encoding of Unicode characters. Its code units have a fixed width, but its encoded scalar values do not."
    },
    {
      "heading": "Surrogate pairs",
      "text": "A surrogate pair consists of:\n\n- A high surrogate, also called a leading surrogate, in the range 0xD800–0xDBFF.\n- A low surrogate, also called a trailing surrogate, in the range 0xDC00–0xDFFF.\n\nTo encode a supplementary code point \\(U\\), first subtract 0x10000:\n\n\\[V = U - \\text{0x10000}.\\]\n\nThe resulting value fits into 20 bits. Its upper and lower ten-bit portions determine the two code units:\n\n\\[H = \\text{0xD800} + \\left\\lfloor \\frac{V}{1024} \\right\\rfloor,\\]\n\n\\[L = \\text{0xDC00} + (V \\bmod 1024).\\]\n\nDecoding reverses this operation:\n\n\\[U = \\text{0x10000} + (H-\\text{0xD800})\\times 1024 + (L-\\text{0xDC00}).\\]\n\nFor example, U+1F600, GRINNING FACE, is encoded as the two code units D83D DE00.\n\nThe high-surrogate, low-surrogate, and ordinary BMP ranges do not overlap. Consequently, a decoder positioned at a code-unit boundary can determine whether a code unit stands alone, begins a pair, or must follow a high surrogate. A surrogate pair has no alternative valid UTF-16 encoding."
    },
    {
      "heading": "Byte order and serialization",
      "text": "UTF-16 defines sequences of 16-bit code units. Storing or transmitting those units as bytes additionally requires an endianness convention:\n\n- UTF-16BE places the most significant byte of each code unit first.\n- UTF-16LE places the least significant byte first.\n\nExamples without an initial byte-order mark are:\n\nCharacter | Code point | UTF-16 code units | UTF-16BE bytes | UTF-16LE bytes\nA | U+0041 | 0041 | 00 41 | 41 00\n€ | U+20AC | 20AC | 20 AC | AC 20\n😀 | U+1F600 | D83D DE00 | D8 3D DE 00 | 3D D8 00 DE\n\nLittle-endian serialization reverses the byte order within each code unit, not the order of the surrogate code units.\n\nByte-order mark\n\nAn initial byte-order mark (BOM), encoded from U+FEFF, can identify byte order:\n\n- FE FF indicates big-endian UTF-16.\n- FF FE indicates little-endian UTF-16.\n\nWhen serving as an encoding signature, this initial value is not part of the textual content.\n\nWhether a BOM is required, permitted, or omitted depends on the file format or protocol. Explicit UTF-16BE and UTF-16LE labels already specify byte order; some specifications prohibit a BOM under those labels. For a generic UTF-16 label without a BOM, the applicable specification determines the interpretation. The Unicode default is big-endian in the absence of a higher-level specification, but software behavior should not be assumed to be uniform.\n\nA BOM is not required for an in-memory sequence whose code-unit representation is already established by its programming environment."
    },
    {
      "heading": "Historical development",
      "text": "Early Unicode design centered on a 16-bit character space. UCS-2, an encoding associated with the early Universal Coded Character Set, represented characters with a single 16-bit unit but could not represent supplementary characters.\n\nUTF-16 retained the direct representation of non-surrogate BMP values while introducing surrogate pairs to extend the available range. Surrogate support was incorporated into Unicode 2.0 in 1996.\n\nFor BMP text outside the surrogate range, UCS-2 and UTF-16 use the same code-unit values. They are nevertheless different encodings: UCS-2 cannot represent supplementary characters, whereas UTF-16 can. Older descriptions of software as using “UCS-2” or “two-byte Unicode” may therefore obscure whether surrogate pairs are supported correctly."
    },
    {
      "heading": "Use in software",
      "text": "UTF-16 is important in operating systems, programming languages, and application programming interfaces.\n\nMicrosoft Windows\n\nMany Unicode interfaces in Microsoft Windows, including the wide-character variants of Windows API functions, use 16-bit code units interpreted as UTF-16. A supplementary character therefore occupies two such units.\n\nThe size and encoding of the C and C++ type wchar_t are implementation-dependent. Its association with UTF-16 on Windows does not make it a portable UTF-16 type across other systems.\n\nJava\n\nJava’s char type is a 16-bit code unit. Its string APIs distinguish operations on char values from operations on Unicode code points. A supplementary character requires two char values.\n\nThis language-level model does not require every Java implementation to store every string internally as an uncompressed array of 16-bit values. Implementations can use alternative storage layouts while preserving the specified behavior.\n\nJavaScript\n\nJavaScript strings are sequences of 16-bit code units. The length property counts those units, not Unicode scalar values or user-perceived characters. For example:\n\n\"😀\".length              // 2\n\"😀\".codePointAt(0)       // 128512, or 0x1F600\n[...\"😀\"].length          // 1\n\nString iteration combines valid surrogate pairs, but it does not segment text into user-perceived characters. A displayed emoji sequence can contain several code points and therefore several iteration elements.\n\nJavaScript strings can also contain lone surrogate code units. Such strings are representable in the language but are not well-formed UTF-16 text."
    },
    {
      "heading": "Text length, indexing, and segmentation",
      "text": "UTF-16 exposes several distinct measures of text length:\n\n- Bytes, determined by serialization.\n- Code units, the 16-bit elements.\n- Unicode scalar values, with a valid surrogate pair counted as one.\n- Extended grapheme clusters, which approximate user-perceived characters.\n\nA grapheme cluster may contain multiple scalar values. For example, a letter followed by a combining accent can form one cluster, while certain emoji sequences contain several supplementary code points and joining characters.\n\nConsequently:\n\n- An index into UTF-16 code units may fall between the halves of a surrogate pair.\n- Truncating at an arbitrary code-unit boundary may produce malformed text.\n- Truncating at a code-point boundary can still split a grapheme cluster.\n- Reversing code units can corrupt surrogate pairs and combining sequences.\n\nIn a flat UTF-16 array, accessing a specified code unit can take constant time. Locating the \\(n\\)th scalar value generally requires scanning or an auxiliary index because preceding values may occupy one or two units. Grapheme-cluster indexing requires additional segmentation rules. These distinctions affect time complexity and the design of text data structures."
    },
    {
      "heading": "Malformed input and security",
      "text": "Well-formed UTF-16 requires every high surrogate to be immediately followed by a low surrogate, and every low surrogate to belong to such a pair. Malformed cases include:\n\n- A high surrogate at the end of input.\n- A high surrogate followed by an ordinary BMP code unit.\n- A low surrogate without a preceding high surrogate.\n- An incomplete code unit in a serialized byte stream.\n\nA decoder may reject malformed input or replace ill-formed portions with U+FFFD, REPLACEMENT CHARACTER, according to its contract. Some APIs preserve unmatched surrogates as raw code units; that behavior does not turn those units into Unicode scalar values.\n\nDifferences between validators, decoders, and downstream processors can create security vulnerabilities. For example, one component may preserve malformed input while another replaces it, causing them to interpret nominally the same identifier differently.\n\nUTF-16 also contains zero-valued bytes in the encodings of many ordinary characters. Byte-oriented functions that expect a zero byte to terminate text cannot process arbitrary UTF-16 correctly. Null termination, where used, is an API convention rather than an intrinsic feature of the encoding."
    },
    {
      "heading": "Comparison with other Unicode encodings",
      "text": "UTF-8\n\nUTF-8 represents a scalar value using one to four bytes. ASCII characters occupy one byte in UTF-8 but two in UTF-16. Many common BMP characters, including much Chinese, Japanese, and Korean text, occupy three bytes in UTF-8 and two in UTF-16. Supplementary characters occupy four bytes in both.\n\nThese sizes describe uncompressed encodings; actual storage also depends on text composition, metadata, and compression. UTF-16 is not byte-compatible with ASCII, whereas UTF-8 preserves ASCII byte values.\n\nUTF-8 has no byte-order distinction. Converting between well-formed UTF-8 and UTF-16 preserves the sequence of scalar values without loss.\n\nUTF-32\n\nUTF-32 uses one 32-bit code unit for every Unicode scalar value. This simplifies scalar-value indexing in a flat array but usually requires more space than UTF-16. It does not make grapheme-cluster indexing constant-time, because user-perceived characters can still contain multiple scalar values.\n\nNeither changing between Unicode encodings nor changing UTF-16 byte order performs Unicode normalization, case conversion, or text segmentation. Those are separate operations on text."
    }
  ],
  "links": [
    "character-encoding",
    "unicode",
    "code-point",
    "bit",
    "byte",
    "byte-order-mark",
    "operating-system",
    "programming-language",
    "application-programming-interface",
    "microsoft-windows",
    "javascript",
    "grapheme-cluster",
    "time-complexity",
    "data-structure",
    "security-vulnerability",
    "utf-eight",
    "ascii"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}