Uint8Array.prototype.toHex()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

toHex()Uint8Array インスタンスのメソッドで、この Uint8Array オブジェクトのデータに基づいて 16 進エンコードされた文字列を返します。

このメソッドは、バイト配列から文字列を作成します。個々の数値を 16 進数に変換するには、代わりに Number.prototype.toString() メソッドを、 radix16 に設定して使用してください。

構文

js
toHex()

引数

なし。

返値

この Uint8Array のデータを表す 16 進エンコードされた文字列。

バイナリーデータのエンコード

この例では、Uint8Array のデータを 16 進文字列にエンコードしています。

js
const uint8Array = new Uint8Array([202, 254, 208, 13]);
console.log(uint8Array.toHex()); // "cafed00d"

const data = new Uint8Array([255, 0, 0, 0, 255, 0, 0, 0, 255]);
for (let i = 0; i < data.length; i += 3) {
  console.log(data.slice(i, i + 3).toHex());
}
// "ff0000"
// "00ff00"
// "0000ff"

仕様書

Specification
Uint8Array to/from base64
# sec-uint8array.prototype.tohex

ブラウザーの互換性

関連情報