MENU
Uint8Array Base64 and Hex
Converting arbitrary binary data (a Uint8Array) to and from text formats like base64 or hexadecimal is an extremely common need (eg. embedding binary data in JSON, or in a data: URI) that previously had no built-in support at all in JavaScript, forcing every project to either write its own converter or pull in a small library. ECMAScript 2026 adds six methods to close this gap.
| Uint8Array.prototype.toBase64([options]) | encodes the bytes as a base64 string |
| Uint8Array.fromBase64(str[, options]) | static; decodes a base64 string into a new Uint8Array |
| Uint8Array.prototype.setFromBase64(str[, options]) | decodes into an existing (eg. pre-allocated) Uint8Array |
| Uint8Array.prototype.toHex() | encodes the bytes as a lowercase hex string |
| Uint8Array.fromHex(str) | static; decodes a hex string into a new Uint8Array |
| Uint8Array.prototype.setFromHex(str) | decodes into an existing Uint8Array |
var bytes = new Uint8Array([72, 101, 108, 108, 111]);
console.log(bytes.toBase64());
console.log(bytes.toHex());
console.log(Uint8Array.fromBase64('SGVsbG8='));
console.log(Uint8Array.fromHex('48656c6c6f'));SGVsbG8=
48656c6c6f
Uint8Array(5) [72, 101, 108, 108, 111]
Uint8Array(5) [72, 101, 108, 108, 111]
The options object accepted by the base64 methods can select an 'alphabet' of 'base64' (the default) or 'base64url' (using - and _ in place of + and /, as used in URLs and JWTs), and toBase64() can omit the trailing = padding with {omitPadding:true}.