TextDecoder: decode() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

TextDecoder.decode() メソッドは、引数として渡されたバッファーからデコードしたテキストを含む文字列を返します。

デコードするメソッドは、現在の TextDecoder オブジェクトで定義されます。 これには、データの予想されるエンコードや、デコードエラーの処理方法などが含まれます。

構文

js
decode()
decode(buffer)
decode(buffer, options)

引数

buffer 省略可

デコードするエンコードされたテキストが入っている、ArrayBufferTypedArrayDataView の何れかのオブジェクトです。

options 省略可

以下のプロパティを持つオブジェクトです。

stream

論理値のフラグで、以後 decode() を呼び出す際に、追加データが続くかどうかを示します。 データを塊で処理する場合は true に設定し、最後の塊、またはデータが塊でない場合は false に設定します。 既定値は false です。

例外

TypeError

TextDecoder.fatal プロパティが true の場合、デコードエラーがあると発生します。

返値

文字列です。

この例では、ユーロ記号 € をエンコードし、デコードしています。

HTML

html
<p>Encoded value: <span id="encoded-value"></span></p>
<p>Decoded value: <span id="decoded-value"></span></p>

JavaScript

js
const encoder = new TextEncoder();
const array = encoder.encode("€"); // Uint8Array(3) [226, 130, 172]
document.getElementById("encoded-value").textContent = array;

const decoder = new TextDecoder();
const str = decoder.decode(array); // String "€"
document.getElementById("decoded-value").textContent = str;

結果

仕様書

Specification
Encoding
# ref-for-dom-textdecoder-decode①

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
decode

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
See implementation notes.
Has more compatibility info.

関連情報