Uint8Array.prototype.toHex()

基準線 2025
新推出

自 ⁨2025 年 9 月⁩起,此功能適用於最新裝置和瀏覽器版本。此功能可能不適用於較舊的裝置或瀏覽器。

Uint8Array 例項的 toHex() 方法返回一個基於此 Uint8Array 物件資料的十六進位制編碼字串。

此方法從位元組陣列建立字串。要將單個數字轉換為十六進位制,請改用將 radix 設定為 16Number.prototype.toString() 方法。

語法

js
toHex()

引數

無。

返回值

一個表示 Uint8Array 中資料的十六進位制編碼字串。

示例

編碼二進位制資料

此示例將 Uint8Array 的資料編碼為十六進位制字串。

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"

規範

規範
Uint8Array 與 base64 的相互轉換
# sec-uint8array.prototype.tohex

瀏覽器相容性

另見