Uint8Array.prototype.toHex()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
The toHex()
method of Uint8Array
instances returns a hex-encoded string based on the data in this Uint8Array
object.
This method creates strings from a byte array. To convert individual numbers into hex, use the Number.prototype.toString()
method with radix
set to 16
instead.
Syntax
js
toHex()
Parameters
None.
Return value
A hex-encoded string representing the data in the Uint8Array
.
Examples
Encoding binary data
This example encodes data from a Uint8Array
into a hex string.
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"
// "00ff00"
Specifications
Specification |
---|
Uint8Array to/from base64 # sec-uint8array.prototype.tohex |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toHex |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
- See implementation notes.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.