Uint8Array base64 and hex conversion
Uint8Array に Base64 および 16進数文字列との相互変換メソッドが追加されます。バイナリデータのエンコード・デコードが標準メソッドで行えるようになります。
概要
Uint8Array に Base64 および 16進数文字列との相互変換メソッドが追加されます。バイナリデータのエンコード・デコードが標準メソッドで行えるようになります。
対応ブラウザ
| 機能 | デスクトップ | モバイル | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 140 | 140 | 133 | 18.2 | 140 | 18.2 | |
| ビルトインオブジェクト | ||||||
| Uint8Array.fromHex() は静的メソッドで、新しい Uint8Array オブジェクトを 16 進文字列から作成します。 | 140 | 140 | 133 | 18.2 | 140 | 18.2 |
| setFromBase64() は Uint8Array インスタンスのメソッドで、この Uint8Array オブジェクトに base64 エンコードされた文字列のバイト列を格納し、読み込んだバイト数と書き込んだバイト数を示すオブジェクトを返します。 | 140 | 140 | 133 | 18.2 | 140 | 18.2 |
| setFromHex() は Uint8Array インスタンスのメソッドで、この Uint8Array オブジェクトを 16 進エンコードされた文字列のバイト列で埋めて、読み込んだバイト数と書き込んだバイト数を示すオブジェクトを返します。 | 140 | 140 | 133 | 18.2 | 140 | 18.2 |
| toBase64() は Uint8Array インスタンスのメソッドで、この Uint8Array オブジェクトのデータに基づいて、 base64 でエンコードされた文字列を返します。 | 140 | 140 | 133 | 18.2 | 140 | 18.2 |
| toHex() は Uint8Array インスタンスのメソッドで、この Uint8Array オブジェクトのデータに基づいて 16 進エンコードされた文字列を返します。 | 140 | 140 | 133 | 18.2 | 140 | 18.2 |
基本構文
// Base64 → Uint8Array
const bytes = Uint8Array.fromBase64('SGVsbG8=');
// Uint8Array [72, 101, 108, 108, 111]
// Uint8Array → Base64
bytes.toBase64(); // 'SGVsbG8='
// Hex conversion
bytes.toHex(); // '48656c6c6f' ライブデモ
Check base64 helpers
Inspect whether Uint8Array base64 helpers are present in the runtime.
Encode bytes when supported
Use toBase64 and toHex if the proposal is implemented.
Compare with TextEncoder
Start from a string and convert it into bytes before encoding.
実務での使いどころ
-
Uint8Array base64 and hex conversion の活用
Uint8Array と Base64/Hex 文字列の相互変換メソッド。fromBase64, toBase64, fromHex, toHex を提供。
注意点
- 古いブラウザ(IE等)では対応していない。対象ブラウザを確認すること。
アクセシビリティ
- JavaScript による動的更新時は、aria-live リージョンで変更をスクリーンリーダーに通知する。
参考リンク
Powered by web-features