-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
Copy pathindex.md
83 lines (57 loc) · 3.55 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
title: TextDecoder
slug: Web/API/TextDecoder
l10n:
sourceCommit: d414c502f3cc1c08d2fb043e98cda4a65621ff08
---
{{APIRef("Encoding API")}}
**`TextDecoder`** インターフェイスは、特定のテキストエンコーディング、例えば `UTF-8`、`ISO-8859-2`、`KOI8-R`、`GBK` などのデコーダーを表します。デコーダーは入力としてバイトストリームを受け取り、コードポイントのストリームを出力します。
{{AvailableInWorkers}}
## コンストラクター
- {{DOMxRef("TextDecoder.TextDecoder", "TextDecoder()")}}
- : 新たに生成した `TextDecoder` を返します。これは、引数で指定したデコード方式を使用して連続したコードポイントを生成します。
## インスタンスプロパティ
_`TextDecoder` インターフェイスは、何もプロパティを継承していません。_
- {{DOMxRef("TextDecoder.encoding")}} {{ReadOnlyInline}}
- : デコーダーの名称を持つ文字列であり、これは `TextDecoder` が使用する方式を表す文字列です。
- {{DOMxRef("TextDecoder.fatal")}} {{ReadOnlyInline}}
- : エラーモードが fatal であるかを示す論理値です。
- {{DOMxRef("TextDecoder.ignoreBOM")}} {{ReadOnlyInline}}
- : [バイトオーダーマーク](https://www.w3.org/International/questions/qa-byte-order-mark)を無視するかどうかを示す論理値です。
## インスタンスメソッド
_`TextDecoder` インターフェイスは、何もメソッドを継承していません。_
- {{DOMxRef("TextDecoder.decode()")}}
- : 特定の `TextDecoder` オブジェクトの方式でデコードされたテキストを含む文字列を返します。
## 例
### 型付き配列でのテキスト表現
この例では、中国語/日本語の文字  を、異なる 5 種類の型付き配列、 {{jsxref("Uint8Array")}}, {{jsxref("Int8Array")}}, {{jsxref("Uint16Array")}}, {{jsxref("Int16Array")}}, {{jsxref("Int32Array")}} で表します。
```js
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));
```
### UTF-8 ではないテキストの扱い
この例では、ロシア語の "Привет, мир!"、 "Hello, world." という意味のテキストをデコードします。 {{domxref("TextDecoder/TextDecoder", "TextDecoder()")}} コンストラクターでは、キリル語の文字に適した Windows-1251 文字エンコーディングを指定します。
```js
const win1251decoder = new TextDecoder("windows-1251");
const bytes = new Uint8Array([
207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33,
]);
console.log(win1251decoder.decode(bytes)); // Привет, мир!
```
## 仕様書
{{Specifications}}
## ブラウザーの互換性
{{Compat}}
## 関連情報
- 逆の操作を表す {{DOMxRef("TextEncoder")}} インターフェイス。
- 対応していないブラウザーでもこのインターフェイスを使用可能にする [shim](https://github.com/inexorabletash/text-encoding)。
- [Node.js supports global export from v11.0.0](https://nodejs.org/api/util.html#util_class_util_textdecoder)