Skip to content
uencoder

UTF-16

UTF-16 is Unicode's variable-length encoding form, using 2 or 4 bytes per code point: BMP characters take one 16-bit code unit and supplementary-plane characters take a surrogate pair (2 code units).

Purpose

It balances space efficiency with full Unicode coverage where BMP characters dominate, such as the Windows API and Java's internal string representation.

Mechanism

A BMP code point (U+0000-FFFF) is stored directly as one 16-bit unit. A supplementary code point (U+10000-10FFFF) has 0x10000 subtracted, then splits into high and low 10-bit halves offset by 0xD800 and 0xDC00 to form a surrogate pair. Byte order is indicated by the BOM (U+FEFF).

Use cases

  • Internal Windows API strings
  • Internal Java/JavaScript string representation
  • Internal .NET String storage
  • File name (NTFS) storage

Examples

  • Input
    A
    Output
    0x00 0x41

    UTF-16BE, 2 bytes

  • Input
    😀
    Output
    0xD8 0x3D 0xDE 0x00

    A surrogate pair, 4 bytes

Common errors

Ignoring surrogate pairs, producing wrong character counts
Use code-point-aware APIs or surrogate-pair-aware iteration

Related terms

Related tools

References