Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
UTF-16
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Code points from U+010000 to U+10FFFF === Code points from the other [[Plane (Unicode)|planes]] are encoded as two 16-bit ''code units'' called a ''surrogate pair''. The first code unit is a ''high surrogate'' and the second is a ''low surrogate'' (These are also known as "leading" and "trailing" surrogates, respectively, analogous to the leading and trailing bytes of UTF-8.<ref name="Unicode7Ch3s8">{{cite book |title=The Unicode Standard, Version 7.0โCore Specification |editor1-last=Allen |editor1-first=Julie D. |editor2-last=Anderson |editor2-first=Deborah |editor3-last=Becker |editor3-first=Joe |editor3-link=Joe Becker (Unicode) |editor4-last=Cook |editor4-first=Richard |publisher=[[Unicode Consortium|The Unicode Consortium]] |date=2014 |place=Mountain View |url=https://www.unicode.org/versions/Unicode7.0.0/ |section=3.8 Surrogates |page=118 |chapter-url=https://www.unicode.org/versions/Unicode7.0.0/ch03.pdf |archive-url=https://ghostarchive.org/archive/20221009/https://www.unicode.org/versions/Unicode7.0.0/ch03.pdf |archive-date=2022-10-09 |url-status=live |access-date=3 November 2014 }}</ref>): {| class="wikitable floatright" style="text-align: center; font-family:monospace" |+ UTF-16 decoder !{{diagonal split header|<span style{{=}}"color:#e91e63;">High</span> | <span style{{=}}"color:#2196f3;">Low</span>}} !<span style="color:#2196f3;">DC00</span> !<span style="color:#2196f3;">DC01</span> !<span style="color:#2196f3;"> ... </span> !<span style="color:#2196f3;">DFFF</span> |- !<span style="color:#e91e63;">D800</span> |010000||010001||...||0103FF |- !<span style="color:#e91e63;">D801</span> |010400||010401||...||0107FF |-align="center" !<span style="color:#e91e63;">โฎ</span> |โฎ||โฎ||โฑ||โฎ |- !<span style="color:#e91e63;">DBFF</span> |10FC00||10FC01||...||10FFFF |} * 0x10000 is subtracted from the code point ''(U)'', leaving a 20-bit number ''(U')'' in the hex number range 0x00000โ0xFFFFF. * The high ten bits (in the range 0x000โ0x3FF) are added to 0xD800 to give the first 16-bit ''code unit'' or ''high surrogate'' ''(W1)'', which will be in the range {{color|#e91e63|0xD800โ0xDBFF}}. * The low ten bits (also in the range 0x000โ0x3FF) are added to 0xDC00 to give the second 16-bit ''code unit'' or ''low surrogate'' ''(W2)'', which will be in the range {{color|#2196f3|0xDC00โ0xDFFF}}. Illustrated visually, the distribution of ''U''' between ''W1'' and ''W2'' looks like:<ref>{{Cite journal|url=https://tools.ietf.org/html/rfc2781.html|title=UTF-16, an encoding of ISO 10646|last1=Yergeau|first1=Francois|last2=Hoffman|first2=Paul|website=tools.ietf.org|date=February 2000|language=en|access-date=2019-06-18}}</ref><syntaxhighlight lang="text"> U' = yyyyyyyyyyxxxxxxxxxx // U - 0x10000 W1 = 110110yyyyyyyyyy // 0xD800 + yyyyyyyyyy W2 = 110111xxxxxxxxxx // 0xDC00 + xxxxxxxxxx </syntaxhighlight> Since the ranges for the ''high surrogates'' ({{color|#e91e63|0xD800โ0xDBFF}}), ''low surrogates'' ({{color|#2196f3|0xDC00โ0xDFFF}}), and valid BMP characters (0x0000โ0xD7FF, 0xE000โ0xFFFF) are [[Disjoint sets|disjoint]], it is not possible for a surrogate to match a BMP character, or for two adjacent ''code units'' to look like a legal ''surrogate pair''. This simplifies searches a great deal. It also means that UTF-16 is ''[[self-synchronizing code|self-synchronizing]]'' on 16-bit words: whether a code unit starts a character can be determined without examining earlier code units (i.e. the type of ''code unit'' can be determined by the ranges of values in which it falls). UTF-8 shares these advantages, but many earlier multi-byte encoding schemes (such as [[Shift JIS]] and other Asian multi-byte encodings) did not allow unambiguous searching and could only be synchronized by re-parsing from the start of the string. UTF-16 is not self-synchronizing if one byte is lost or if traversal starts at a random byte. Because the most commonly used characters are all in the BMP, handling of surrogate pairs is often not thoroughly tested. This leads to persistent bugs and potential security holes, even in popular and well-reviewed application software (e.g. {{CVE|2008-2938|2012-2135}}).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)