Uint8Array Base64/Hexコンバーターとは
Uint8Array.prototype.toBase64() encodes the byte contents of a Uint8Array to a Base64-encoded string. Uint8Array.prototype.toHex() produces a lowercase hex string. Their counterparts, Uint8Array.fromBase64(string) and Uint8Array.fromHex(string), parse Base64 and hex strings back into Uint8Array. These methods are part of Baseline 2025 (Chrome 136+) and eliminate the need for manual encoding helpers. They are the standard way to transfer binary data between the browser and the server or between different Web APIs.
クイックアンサー
Generate code snippets using native Uint8Array Base64 and Hex encoding methods. toBase64() and toHex() encode binary data; Uint8Array.fromBase64() and Uint8Array.fromHex() decode strings back to binary. Available in Chrome 136+, Edge, Firefox, Safari. No more manual btoa or charCodeAt loops.
制限事項
- Not available in older browsers without Baseline 2025 support. For broad compatibility, provide a fallback using manual encoding via btoa and charCodeAt until the methods reach all supported environments.
- Decoding very large Base64 or hex strings into Uint8Array allocates the full decoded buffer in memory and can cause out-of-memory conditions. For extremely large data, consider streaming decoders or chunked processing.
- Only standard Base64 with padding is supported by fromBase64(). URL-safe Base64, Base64 without padding, and hex strings require pre-processing before using the native methods.
使い方
- Create a Uint8Array from file data, typed array operations, or a Web API like fetch, WebSocket, or FileReader.
- Call .toBase64() on the array to get a standard Base64 string suitable for JSON payloads, data URIs, or API requests.
- Call .toHex() on the array to get a hex string useful for debugging, cryptographic display, or protocols that expect hex encoding.
- Use Uint8Array.fromBase64(string) or Uint8Array.fromHex(string) to decode the encoded string back to binary data.
主な用途
- Encode binary file contents uploaded from a file input as Base64 for sending in a JSON API request.
- Display raw byte data as a hex string in a developer tool or debug panel for inspection.
- Decode a Base64-encoded response from a WebSocket or fetch API back into a Uint8Array for further binary processing.