Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Uint8Array.fromHex()

Limited availability

TheUint8Array.fromHex() static method creates a newUint8Array object from a hexadecimal string.

This method parses the string into a byte array. To convert the string into a single number, use theparseInt() function withradix set to16 instead.

Syntax

js
Uint8Array.fromHex(string)

Parameters

string

A hexadecimal string encoding bytes to convert to aUint8Array. The string must:

  • Have an even number of characters because two characters encode one byte.
  • Only contain characters in the hexadecimal alphabet, which includes 0–9 and A–F (case-insensitive).
  • Not contain whitespace (unlikeUint8Array.prototype.setFromBase64()).

Return value

A newUint8Array object containing the decoded bytes from the hexadecimal string.

Exceptions

SyntaxError

Thrown if the input string contains characters outside the hex alphabet, or its length is odd.

TypeError

Thrown if the input string is not a string.

Examples

Decoding a hexadecimal string

This example decodes a hexadecimal string into aUint8Array.

js
const hexString = "cafed00d";const bytes = Uint8Array.fromHex(hexString);console.log(bytes); // Uint8Array [ 202, 254, 208, 13 ]

Uppercase characters are also supported:

js
const hexString = "CAFEd00d";const bytes = Uint8Array.fromHex(hexString);console.log(bytes); // Uint8Array [ 202, 254, 208, 13 ]

Specifications

Specification
Uint8Array to/from base64
# sec-uint8array.fromhex

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp