[23] [DFN[[[UTF-16]]]] は、 [[Unicode]] の[[符号化方式]]の1つです。

* 代替

[32] 新たに [[UTF-16]] を使うべきではありません。かわりに、 [[UTF-8]]
を使うのが好ましいとされています。

* 仕様書

[REFS[
- [22] [CITE@en-US[Encoding Standard]] ([TIME[2014-07-11 16:52:59 +09:00]] 版) <http://encoding.spec.whatwg.org/#common-infrastructure-for-utf-16be-and-utf-16le>
- [25] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2014-07-20 22:41:10 +09:00]] 版) <http://www.whatwg.org/specs/web-apps/current-work/#a-utf-16-encoding>
- [35] [CITE@en[Fix #18: remove UTF-16 support from TextEncoder]] ([[annevk]]著, [TIME[2016-03-16 01:21:27 +09:00]]) <https://github.com/whatwg/encoding/commit/8360f775c8df145f649047c7d59c5ff733ade112>
]REFS]

[36] 既存のデータとの互換性のため、 [CITE[Encoding Standard]] [SRC[>>22]] で
[[UTF-16]] の[[復号]]が規定されています。以前 [SRC[>>35]] は[[符号化]]も規定されていましたが、
[[Web互換性]]のためには不要であるとして、削除されました。

* 名前

[21] [[Encoding Standard]] によれば、[[大エンディアン]]の [[UTF-16]]
の名前が [DFN[[[UTF-16BE]]]] であり、[[ラベル]]として [DFN[[CODE@en[[[utf-16be]]]]]]
を使って指定できます。また[[小エンディアン]]の [[UTF-16]] の名前が [DFN[[[UTF-16LE]]]]
であり、[[ラベル]]として [DFN[[CODE@en[[[utf-16le]]]]]] または
[DFN[[CODE@en[[[utf-16]]]]]] を使って指定できます。

* UTF-16 符号化

[24] [[HTML Standard]] は [CODE[[[UTF-16BE]]]], [CODE[[[UTF-16LE]]]] [DEL[, [CODE[[[utf-16]]]]]] の総称を [DFN[[RUBYB[UTF-16 符号化]@en[a UTF-16 encoding]]]]としています [SRC[>>25]]。

[HISTORY[
[20] [[RFC 3023]] は、 [[RFC 2781]] における [CODE(charset)@en[[[utf-16]]]]、
[CODE(charset)@en[[[utf-16be]]]]、[CODE(charset)@en[[[utf-16le]]]]
の総称を [DFN[[RUBYB[UTF-16 族]@en[the UTF-16 family]]]]としています [SRC[>>19]]。
]HISTORY]

* 符号化方式

= [6] [CODE(char)[[[U+D800]]]] から [CODE(char)[[[U+DFFF]]]] までは表現できません。
= [7] [CODE(char)[[[U-00110000]]]] から先は表現できません。 ([[ISO/IEC 10646]] には存在しますが、[[Unicode]] には存在しません。)
= [8] それ以外で [CODE(char)[[[U+0000]]]] から [CODE(char)[[[U+FFFF]]]] であるなら、その[[符号位置]]と同じ値の[[16ビット符号単位]]によって表現します。
= [9] それ以外なら、つまり [CODE(char)[[[U+10000]]]] から [CODE(char)[[[U+10FFFF]]]] であるなら、
== [10] [VAR[U']] を[[符号位置]]から 0x10000 引いた値とします。この結果の値は20ビット非負整数です。
== [11] [VAR[W1]] を [VAR[U']] の上位10ビットと 0xD800 の[[論理和]]とします。
== [12] [VAR[W2]] を [VAR[U']] の下位10ビットと 0xDC00 の[[論理和]]とします。
== [13] 当該[[符号位置]]は [VAR[W1]] [VAR[W2]] の2つの[[16ビット符号単位]]の列によって表現します。

[14] >>9 を図示すると、次のようになります。

- [VAR[U']] = 0b[VAR[yyyyyyyyyy]][VAR[xxxxxxxxxx]]
- [VAR[W1]] = 0b110110[VAR[yyyyyyyyyy]]
- [VAR[W2]] = 0b110111[VAR[xxxxxxxxxx]]

* BOM

[26] [[UTF-16]] および派生[[文字コード]]での [[BOM]] の取り扱いについては、
[[BOM]] の項を参照してください。

* 文脈

[FIG(middle list)[ [54] [[UTF-16]] を採用

- [[JavaScript]]
- [[DOMString]]
- [[Java]]
- [[XML]]
- [[Xceed Unicode extra field]] ([[UTF-16LE]])
- [[DOMHASH]] ([[UTF-16BE]])
- [[LeXML]] ([[UTF-16]] を推奨)
- [[Windows]] ([[UTF-16LE]])
-[40] 
[[PDF]]
([CN[BOM]]
付き
[[UTF-16BE]])
- 
[41] 
[[OpenType]]
([[UTF-16BE]])

]FIG]

** Web における UTF-16

[29] [[Web]] の[[文字コード]]は [[UTF-8]] であり、 [[UTF-16]] は使うべきではないとされています。

[30] ただし [[JavaScript]] や [[DOM]] は、歴史的理由により、[[文字列]]の表現に
[[UTF-16]] による[[16ビット符号単位]]を使っています。

** XML における UTF-16

[27] [[XML]] には [[UTF-16]] に関する特別な規定がいくつもあります。
詳しくは [[XMLにおける文字コード]]を参照してください。



** Python における UTF-16

[SEE[ [[surrogateescape]] ]]



* 実利用例

- [51] [CITE[i-DNS.net International]], [TIME[2004-11-04T05:47:26.000Z]], [TIME[2025-06-24T12:55:07.715Z]] <http://www.i-dns.net/samples/tamil.html>
-- [49] [CITE[i-DNS.net International]], [TIME[2025-06-24T12:54:23.000Z]] <https://web.archive.org/web/20060113220809im_/http://www.i-dns.net/samples/tamil.html>
-- [50] [[UTF-16LE]]
- [52] [CITE[Greek and Hebrew Encoding Converter: Unicode to and from legacy encodings (fonts)]], [TIME[2025-09-06T12:14:18.000Z]] <http://web.archive.org/web/20071214194949im_/http://ocp.acadiau.ca/kpenner/convert.htm>
-- [53] [[UTF-16LE]]

* 歴史

[37] [[UTF-16]] は、 [[UCS-2]] の16ビット平面ではすべての[[文字]]の[[符号化]]に十分でないことが明確になったとして[[サロゲートペア]]が導入されたことにより登場しました。

[45] 
[CITE@ja[UTF-16の誕生 | yasuokaの日記 | スラド]], [TIME[2024-01-31T14:16:35.000Z]] <https://srad.jp/~yasuoka/journal/387747/>

[47] [CITE[Wayback Machine]], [TIME[2025-02-15T08:17:20.000Z]] <https://web.archive.org/web/20000821235014/http://www.unicode.org/Public/TEXT/ALLOC.TXT>

>In the ISO-IEC SC2/WG2 meetings where the current structure of the 10646 BMP
was decided, there was discussion of the potential use of the O-Zone as a
target zone for swapping in designated quarters of supplemental planes.  The
topic was however deferred, and no formal definition of such a regime was
defined.  The Japan national body negative vote on DIS 10646-1.2 explicitly
requested that the O-Zone be expanded from 64 to 94 rows "for swapping-in the
existing standards"; again no formal details were provided.  In the absence
of detailed proposals, general comments may be made on these two broad
variants of the swap zone idea.
>
Considering the Japanese innovation first, this proposal to expand the O-Zone
and swap in existing standards was explicitly rejected by WG2, because it is
opposed to the basic intent of this standard, which is to assign a single code
position to each graphical character, not to provide an indexing mechanism
into existing standards.  The expansion of the O-Zone to 94 rows is impossible
in any case.

[48] [[日本]]はこの段階でもまだ [[DIS 10646]] みたいなのをぶっこもうとしてたのか、攻めてるなあw

** ISO/IEC 10646 における UTF-16

[1]
UTF-16 は、 [[ISO/IEC 10646]] の附属書 C で規定されている [[UCS]] の[[符号化表現]]の一つです。

[2]
[[送信装置]]は、[[受信装置]]が [[UCS-2]] しか解釈できない場合でも UTF-16 を送って構いません。 (C.7 参照。)

[3]
C.7 は、 UTF-16/UCS-2 の実装を次のように分類しています。

(00 群 01 面以降を表す[[区点要素]]対の表示方法)
,	,記号	,知らない文字	,知っている文字
,UCS-2の実装	,U	,〓〓	,−
,弱い実装	,W	,〓〓	,α
,中程度の強さの実装	,A	,〓	,α
,強い実装	,S	,〓	,α

([SAMP(char)[α]]が 00 群 01 面以降にある知っている文字、 [SAMP(char)[〓]]は知らない文字の代替表現とする。)

[4]
>>3 W の実装ってあるのだろうか?

[5] 中程度の強さの実装と強い実装の違いはなんだ?

** UTF-16x

[43] 
[CITE[[DFN[UTF-16x]] (UTF-16 extension)]], 
Apr.30 1999,
[TIME[2006-11-13T17:59:50.000Z]], [TIME[2022-10-11T03:43:32.172Z]] <http://hp.vector.co.jp/authors/VA002891/UTF16X.TXT>

;; [44] [[Shift-Mojikyo]] も参照。

** RFC

[15] [CITE@en[RFC 2781 - UTF-16, an encoding of ISO 10646]]
( ([TIME[2011-02-03 23:57:14 +09:00]] 版))
<http://tools.ietf.org/html/rfc2781>

[19] [CITE@en[RFC 3023 - XML Media Types]] ([TIME[2014-07-11 12:46:44 +09:00]] 版) <http://tools.ietf.org/html/rfc3023#section-2>


[46] 
[CITE@en[335531 - Correct misuse of "UTF-16BE", "UTF-16LE", "UTF-32BE", and "UTF-32LE" charset labels]], [TIME[2025-12-01T08:09:11.000Z]] <https://bugzilla.mozilla.org/show_bug.cgi?id=335531>

[55] 
[CITE@en[235090 - CSS stylesheets labeled as "UTF-16" don't load]], [TIME[2025-12-01T08:18:28.000Z]] <https://bugzilla.mozilla.org/show_bug.cgi?id=235090>


[56] 
[CITE@en[68738 - (BOM) UTF-XX with BOM always maps to UTF-XXBE]], [TIME[2025-12-01T08:20:43.000Z]] <https://bugzilla.mozilla.org/show_bug.cgi?id=68738>


** Encoding Standard

[16] [CITE@en[Web Applications 1.0 r6498 Clean up how we refer to UTF-16.]]
( ([TIME[2011-08-18 07:28:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=6497&to=6498>

[17] [CITE[IRC logs: freenode / #whatwg / 20111226]]
( ([TIME[2011-12-27 08:38:44 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20111226#l-109>

[18] [CITE[''''''[''''''whatwg'''''']'''''' '''['''encoding''']''' utf-16]]
( ([TIME[2011-12-28 00:21:19 +09:00]] 版))
<http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2011-December/034260.html>

[28] [CITE@en[Describe the security situation around encodings and require browsers to... · 2e43ead · whatwg/encoding]]
( ([TIME[2014-12-07 20:14:32 +09:00]] 版))
<https://github.com/whatwg/encoding/commit/2e43ead5c796e314cd3aaada10a2dc33de7bfaf1#diff-8d4d847e6257b75f4bf8030496281de4R2632>

[31] [CITE@en[24120 – The Encoding Standard only recognizes utf-16le and utf-16be. This should just refer to those if this is needed at all.]]
([TIME[2015-09-03 14:37:32 +09:00]] 版)
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=24120#c6>

[33] [CITE@en[Update integration with Encoding Standard · whatwg/html@6a31c26]]
([TIME[2016-02-14 18:36:40 +09:00]] 版)
<https://github.com/whatwg/html/commit/6a31c26cf12e39dab1a488e75dd56c03d6786d39>

[34] [CITE@en[Fix #18: remove UTF-16 support from TextEncoder · whatwg/encoding@8360f77]]
([TIME[2016-03-19 14:08:21 +09:00]] 版)
<https://github.com/whatwg/encoding/commit/8360f775c8df145f649047c7d59c5ff733ade112>

[38] [CITE@en[Editorial: check non-null before null]]
([[annevk]]著, [TIME[2017-05-05 21:01:38 +09:00]])
<https://github.com/whatwg/encoding/commit/4e53e160b9e0ade6e33a25d21580106a0db2c47f>

[39] [CITE[XLIFF Version 2.0]]
([TIME[2014-08-06 01:00:00 +09:00]])
<http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#d0e15952>


[42] 
[CITE@en[809934 - Remove reliance on BOM-sniffing UTF-16 decoder]], [TIME[2025-12-01T08:07:06.000Z]] <https://bugzilla.mozilla.org/show_bug.cgi?id=809934>


