--- title: TextDecoder slug: Web/API/TextDecoder tags: - API - DOM - Encoding - Experimental - Interface - Reference - TextDecoder translation_of: Web/API/TextDecoder --- <p>{{APIRef("Encoding API")}}</p> <p><code><strong>TextDecoder</strong></code> インターフェイスは特定のテキストエンコーディング、例えば <code>UTF-8</code>, <code>ISO-8859-2</code>, <code>KOI8-R</code>, <code>GBK</code>, 等のデコーダーを表します。デコーダーは入力としてバイトのストリームを取り、コードポイントのストリームを出力します。</p> <h2 id="ExamplesExample" name='ExamplesExample"'>例</h2> <h3 id="Representing_text_with_typed_arrays" name="Representing_text_with_typed_arrays">型付き配列でのテキスト表現</h3> <p>この例では、中国語/日本語の文字 <img alt="" src="https://mdn.mozillademos.org/files/16663/2019-05-21_191907.png" style="height: 32px; width: 41px;"> を、異なる 5 種類の型付き配列、 {{jsxref("Uint8Array")}}, {{jsxref("Int8Array")}}, {{jsxref("Uint16Array")}}, {{jsxref("Int16Array")}}, {{jsxref("Int32Array")}} で表します。</p> <pre class="brush: js; notranslate">let utf8decoder = new TextDecoder(); // default 'utf-8' or 'utf8' let u8arr = new Uint8Array([240, 160, 174, 183]); let i8arr = new Int8Array([-16, -96, -82, -73]); let u16arr = new Uint16Array([41200, 47022]); let i16arr = new Int16Array([-24336, -18514]); let i32arr = new Int32Array([-1213292304]); console.log(utf8decoder.decode(u8arr)); console.log(utf8decoder.decode(i8arr)); console.log(utf8decoder.decode(u16arr)); console.log(utf8decoder.decode(i16arr)); console.log(utf8decoder.decode(i32arr)); </pre> <h3 id="Handling_non-UTF8_text" name="Handling_non-UTF8_text">UTF8 ではないテキストの扱い</h3> <p>この例では、ロシア語の "Привет, мир!"、 "Hello, world." という意味のテキストをデコードします。 {{domxref("TextDecoder/TextDecoder", "TextDecoder()")}} コンストラクターでは、キリル語の文字に適した Windows-1251 文字エンコーディングを指定します。</p> <pre class="brush: js; notranslate">let win1251decoder = new TextDecoder('windows-1251'); let bytes = new Uint8Array([207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33]); console.log(win1251decoder.decode(bytes)); // Привет, мир! </pre> <h2 id="Constructor" name="Constructor">コンストラクター</h2> <dl> <dt>{{DOMxRef("TextDecoder.TextDecoder", "TextDecoder()")}}</dt> <dd>新たに生成した <code>TextDecoder</code> を返します。これは、引数で指定したデコード方式を使用して連続したコードポイントを生成します。</dd> </dl> <h2 id="Properties" name="Properties">プロパティ</h2> <p><em><code>TextDecoder</code> インターフェイスは、何もプロパティを継承していません。</em></p> <dl> <dt>{{DOMxRef("TextDecoder.prototype.encoding")}}{{ReadOnlyInline}}</dt> <dd>デコーダーの名称を持つ {{DOMxRef("DOMString")}} であり、これは <code>TextDecoder</code> が使用する方式を表す文字列です。</dd> <dt>{{DOMxRef("TextDecoder.prototype.fatal")}}{{ReadOnlyInline}}</dt> <dd>エラーモードが fatal であるかを示す {{jsxref('Boolean')}} です。</dd> <dt>{{DOMxRef("TextDecoder.prototype.ignoreBOM")}}{{ReadOnlyInline}}</dt> <dd>バイトオーダーマークを無視するかどうかを示す {{jsxref('Boolean')}} です。</dd> </dl> <h2 id="Methods" name="Methods">メソッド</h2> <p><em><code>TextDecoder</code> インターフェイスは、何もメソッドを継承していません。</em></p> <dl> <dt>{{DOMxRef("TextDecoder.prototype.decode()")}}</dt> <dd>特定の <code>TextDecoder</code> オブジェクトの方式でデコードされたテキストを含む {{domxref("DOMString")}} を返します。</dd> </dl> <h2 id="Specifications" name="Specifications">仕様書</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様書</th> <th scope="col">状態</th> <th scope="col">備考</th> </tr> </thead> <tbody> <tr> <td>{{SpecName("Encoding", "#interface-textdecoder", "TextDecoder")}}</td> <td>{{Spec2("Encoding")}}</td> <td>初回定義</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <div> <p>{{Compat("api.TextDecoder")}}</p> </div> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>逆の操作を表す {{DOMxRef("TextEncoder")}} インターフェイス。</li> <li><a href="/ja/Add-ons/Code_snippets/StringView"><code>StringView</code></a> – 型付き配列による、C ライクな文字列の表現</li> <li>非サポートブラウザーでもこのインターフェイスを使用可能にする <a href="https://github.com/inexorabletash/text-encoding">shim</a>。</li> <li><code><a href="/ja/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.importGlobalProperties">Components.utils.importGlobalProperties</a></code></li> <li><a href="https://nodejs.org/api/util.html#util_class_util_textdecoder">Node.js supports global export from v11.0.0</a></li> </ul>