Uint8Array.fromHex()
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.
Uint8Array.fromHex()
は静的メソッドで、新しい Uint8Array
オブジェクトを 16 進文字列から作成します。
このメソッドは、文字列をバイト配列に解釈します。文字列を単一の数値に変換するには、代わりに parseInt()
関数を、 radix
を 16
に設定して使用してください。
構文
js
Uint8Array.fromHex(string)
引数
string
-
Uint8Array
に変換するバイト列をエンコードした 16 進文字列。文字列は次の条件を満たす必要があります。- 2 文字で 1 バイトをエンコードするため、文字数は偶数である必要があります。
- 0~9 および A~F (大文字と小文字を区別しない)の 16 進文字のみが含まれている必要があります。
- ホワイトスペースが含まれていないこと(
Uint8Array.prototype.setFromBase64()
とは異なります)。
返値
16 進文字列からデコードされたバイトが含まれている新しい Uint8Array
オブジェクト。
例外
SyntaxError
-
入力文字列に 16 進文字以外の文字が含まれているか、その長さが奇数の場合に発生します。
TypeError
-
入力文字列が文字列でない場合に発生します。
例
>16 進文字列のデコード
この例では、16 進文字列を Uint8Array
にデコードしています。
js
const hexString = "cafed00d";
const bytes = Uint8Array.fromHex(hexString);
console.log(bytes); // Uint8Array [ 202, 254, 208, 13 ]
大文字も対応しています。
js
const hexString = "CAFEd00d";
const bytes = Uint8Array.fromHex(hexString);
console.log(bytes); // Uint8Array [ 202, 254, 208, 13 ]
仕様書
Specification |
---|
Uint8Array to/from base64> # sec-uint8array.fromhex> |
ブラウザーの互換性
Loading…