UTF-16 is a variable-length Unicode character encoding that represents each code point as either one or two 16-bit code units, for a total of 2 or 4 bytes per character. In software interoperability, UTF-16 matters because it is the native string format of several major platforms, including the Windows API, Java, JavaScript and .NET, so text exchanged between these environments and byte-oriented systems often has to be converted.
UTF-16 sits between UTF-8, the dominant encoding for files and network protocols, and UTF-32, which uses a fixed four bytes per code point. Its trade-offs around byte order, surrogate pairs and string length calculation are a common source of mojibake, truncated text and subtle bugs when data crosses system boundaries.
How UTF-16 encodes code points
UTF-16 encodes Unicode code points in two ways depending on their value. Code points from U+0000 to U+FFFF, the Basic Multilingual Plane (BMP), are stored directly as a single 16-bit code unit, except for the reserved range U+D800 to U+DFFF. This range covers most modern scripts, including Latin, Cyrillic, Greek, Arabic and the common CJK ideographs.
Code points from U+10000 to U+10FFFF, the supplementary planes, are encoded as a surrogate pair. The encoder subtracts 0x10000, splits the remaining 20 bits into two 10-bit halves, and adds them to a high surrogate base of 0xD800 and a low surrogate base of 0xDC00. Emoji, historic scripts and rare ideographs all require this four-byte form.
The table below shows how sample characters map to UTF-16 code units and how their size compares with other encodings.
| Character | Code point | UTF-16 code units | UTF-16 bytes | UTF-8 bytes |
|---|---|---|---|---|
| Latin A | U+0041 | 0041 | 2 | 1 |
| Euro sign | U+20AC | 20AC | 2 | 3 |
| CJK ideograph | U+4E2D | 4E2D | 2 | 3 |
| Grinning face emoji | U+1F600 | D83D DE00 | 4 | 4 |
| Musical G clef | U+1D11E | D834 DD1E | 4 | 4 |
Because the surrogate ranges are reserved and never assigned to characters, a decoder can always tell a lone BMP unit from half of a pair, which makes UTF-16 self-synchronizing at the code unit level.
Byte order and the byte order mark
A 16-bit code unit occupies two bytes, so serializing UTF-16 requires a decision about endianness. UTF-16BE stores the most significant byte first, UTF-16LE stores the least significant byte first, and plain UTF-16 leaves the order to be signaled by a byte order mark (BOM) or by protocol convention.
The BOM is the character U+FEFF placed at the start of a stream. Read in the correct order it appears as FE FF for big-endian or FF FE for little-endian. The Unicode Standard specifies that unmarked UTF-16 defaults to big-endian, yet in practice Windows tools commonly write little-endian with a BOM.
This table lists the encoding labels that appear in file headers, APIs and MIME type parameters.
| Label | Byte order | BOM expected | Leading bytes of the letter A |
|---|---|---|---|
| UTF-16BE | Big-endian | No, order is fixed by label | 00 41 |
| UTF-16LE | Little-endian | No, order is fixed by label | 41 00 |
| UTF-16 with BE BOM | Big-endian | Yes | FE FF 00 41 |
| UTF-16 with LE BOM | Little-endian | Yes | FF FE 41 00 |
| UCS-2 | Platform dependent | Optional | Same as UTF-16 for BMP text |
A mismatch between the declared and actual byte order produces swapped characters, often seen as CJK-looking garbage in place of English text, which is a classic form of mojibake.
UTF-16 compared with UTF-8 and UTF-32
All three Unicode Transformation Formats encode the same repertoire of code points and convert to one another without loss. They differ in unit size, storage efficiency and compatibility with byte-oriented software. Choosing among them is mainly an interoperability decision rather than a question of which characters can be represented.
| Property | UTF-8 | UTF-16 | UTF-32 |
|---|---|---|---|
| Code unit size | 8 bits | 16 bits | 32 bits |
| Bytes per code point | 1 to 4 | 2 or 4 | Always 4 |
| ASCII compatible | Yes | No | No |
| Endianness issue | None | Yes | Yes |
| Size for English text | Smallest | About double UTF-8 | About four times UTF-8 |
| Size for East Asian text | Usually 3 bytes per character | Usually 2 bytes per character | 4 bytes per character |
| Typical home | Web, files, Linux, protocols | Windows API, Java, JavaScript strings | Internal processing |
UTF-16 contains many zero bytes in Latin text, which breaks C-style string functions that treat a zero byte as the terminator. That is why UTF-8 dominates file formats and network protocols, while UTF-16 remains common inside runtimes and operating system APIs.
Historical origin and UCS-2 legacy
UTF-16 grew out of UCS-2, a fixed-width 16-bit encoding from the early 1990s, when designers expected that 65,536 code points would be enough for all scripts. Windows NT, Java and early JavaScript engines adopted 16-bit characters on that assumption.
When Unicode 2.0 extended the code space to 1,114,112 code points, surrogate pairs were introduced so that existing 16-bit systems could reach the new planes without changing their string layout. Many APIs still behave like UCS-2: they count code units rather than characters, so a single emoji reports a length of 2 in JavaScript and Java.
Interoperability pitfalls and safe practice
Most UTF-16 problems appear at the boundary between systems, where one side assumes bytes and the other assumes 16-bit units. The table below maps typical symptoms to their causes and recommended remedies.
| Symptom | Cause | Remedy |
|---|---|---|
| Text appears as spaced letters or null characters | UTF-16 read as single-byte text | Detect the BOM or declare the encoding |
| English shows as CJK symbols | Wrong byte order | Use explicit UTF-16LE or UTF-16BE labels |
| Emoji split into two broken symbols | Truncation between surrogates | Cut strings at code point boundaries |
| String length larger than visible characters | Counting code units | Iterate by code point or grapheme cluster |
| Conversion error on export | Unpaired surrogate in source | Validate or replace with U+FFFD |
- Prefer UTF-8 for files, APIs and storage exchanged with other systems, and convert to UTF-16 only at platform boundaries.
- Always label the byte order explicitly when UTF-16 must be written to disk or the network.
- Treat lone surrogates as invalid data, because strict UTF-8 encoders reject them.
- Remember that user-perceived characters may span several code points, so even code point iteration is not enough for cursor movement.
A plain-language summary
Computers store letters as numbers, and UTF-16 is one agreed way of writing those numbers down. It uses building blocks of two bytes each. Most everyday characters fit into one block, while rarer ones such as emoji need two blocks joined together.
Trouble starts when two programs disagree about how to read the blocks, much like reading a date as day-month instead of month-day. The text then turns into nonsense. The safest habit is to state clearly which encoding a file uses, and to use UTF-8 when sharing text widely.
An example from practice
A data team exports a customer list from a Windows desktop application into a CSV file. The application writes the file as UTF-16LE with a BOM, the default for its export routine. When the file is loaded into a Linux-based import pipeline that expects UTF-8, every name appears with a null byte between letters, and the import rejects 4,200 rows as malformed.
An engineer inspects the first bytes, sees FF FE, and adds a conversion step that decodes UTF-16LE and re-encodes as UTF-8 before parsing. A second issue surfaces: a few customer notes contain emoji that were cut in half by a 255-unit field limit in the source application, leaving unpaired surrogates. The pipeline now replaces them with U+FFFD and logs each case. The result is a clean import of all rows and a documented encoding contract for future exports.