Uint8Array.prototype.toHex()

Baseline 2025
Newly available

Since September 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Phương thức toHex() của các instance Uint8Array trả về một chuỗi được mã hóa hex dựa trên dữ liệu trong đối tượng Uint8Array này.

Phương thức này tạo chuỗi từ một mảng byte. Để chuyển đổi từng số thành hex, hãy dùng phương thức Number.prototype.toString() với radix16 thay thế.

Cú pháp

js
toHex()

Tham số

Không có.

Giá trị trả về

Một chuỗi mã hóa hex đại diện cho dữ liệu trong Uint8Array.

Ví dụ

Mã hóa dữ liệu nhị phân

Ví dụ này mã hóa dữ liệu từ Uint8Array thành chuỗi hex.

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"

Đặc tả

Specification
ECMAScript® 2027 Language Specification
# sec-uint8array.prototype.tohex

Tương thích trình duyệt

Xem thêm