<<<<<<< HEAD
もしバイナリデータが実際には文字列だったらどうしますか?例えばテキストデータを持つファイルを受け取りました。
組み込みの TextDecoder オブジェクトを使うと、指定されたバッファとエンコーディングから、値を実際の JavaScript 文字列として読むことができます。
What if the binary data is actually a string? For instance, we received a file with textual data.
The built-in TextDecoder object allows one to read the value into an actual JavaScript string, given the buffer and the encoding.
We first need to create it:
746ad803c878e33182e7fab1578c0d15b9b75ca0
let decoder = new TextDecoder([label], [options]);<<<<<<< HEAD
label-- エンコーディング、デフォルトではutf-8ですが、big5,windows-1251やその他多くをサポートしています。options-- オプションのオブジェクト:fatal-- boolean,trueの場合、無効(デコード不可能)な文字の場合は例外をスローし、そうでなければ(デフォルト)、それらを\uFFFDに置き換えます。ignoreBOM-- boolean,trueの場合、BOM (オプションのバイトオーダーマーク)を無視します。めったに使われません。
label-- the encoding,utf-8by default, butbig5,windows-1251and many other are also supported.options-- optional object:fatal-- boolean, iftruethen throw an exception for invalid (non-decodable) characters, otherwise (default) replace them with character\uFFFD.ignoreBOM-- boolean, iftruethen ignore BOM (an optional byte-order Unicode mark), rarely needed.
...And then decode:
746ad803c878e33182e7fab1578c0d15b9b75ca0
let str = decoder.decode([input], [options]);<<<<<<< HEAD
input-- デコード対象のBufferSourceですoptions-- オプションのオブジェクト:stream--decoderが入ってくるデータのチャンクに対し繰り返し呼び出されるとき、ストリームをデコードする場合はtrueです。この場合、マルチバイト文字がチャンク間で分割される可能性があります。このオプションは "未完了" の文字を記憶して、次のチャンクが来た時にそれらをデコードするようにTextDecoderに伝えます。
input--BufferSourceto decode.options-- optional object:stream-- true for decoding streams, whendecoderis called repeatedly with incoming chunks of data. In that case a multi-byte character may occasionally split between chunks. This options tellsTextDecoderto memorize "unfinished" characters and decode them when the next chunk comes.
For instance:
746ad803c878e33182e7fab1578c0d15b9b75ca0
let uint8Array = new Uint8Array([72, 101, 108, 108, 111]);
alert( new TextDecoder().decode(uint8Array) ); // Hellolet uint8Array = new Uint8Array([228, 189, 160, 229, 165, 189]);
alert( new TextDecoder().decode(uint8Array) ); // 你好We can decode a part of the buffer by creating a subarray view for it:
746ad803c878e33182e7fab1578c0d15b9b75ca0
let uint8Array = new Uint8Array([0, 72, 101, 108, 108, 111, 0]);
<<<<<<< HEAD
// 文字列が中央にあります
// コピーせずに、新しいビューを作成します
=======
// the string is in the middle
// create a new view over it, without copying anything
>>>>>>> 746ad803c878e33182e7fab1578c0d15b9b75ca0
let binaryString = uint8Array.subarray(1, -1);
alert( new TextDecoder().decode(binaryString) ); // Hello<<<<<<< HEAD TextEncoder は逆のこと -- 文字列をバイトにします。
構文は次の通りです:
let encoder = new TextEncoder();サポートしているエンコーディングは "utf-8" だけです。
2つのメソッドがあります:
encode(str)-- 文字列からUint8Arrayを返します。encodeInto(str, destination)--strをdestinationにエンコードします。destinationはUint8Arrayでなければなりません。 ======= TextEncoder does the reverse thing -- converts a string into bytes.
The syntax is:
let encoder = new TextEncoder();The only encoding it supports is "utf-8".
It has two methods:
encode(str)-- returnsUint8Arrayfrom a string.encodeInto(str, destination)-- encodesstrintodestinationthat must beUint8Array.
746ad803c878e33182e7fab1578c0d15b9b75ca0
let encoder = new TextEncoder();
let uint8Array = encoder.encode("Hello");
alert(uint8Array); // 72,101,108,108,111