If your data may contain multi-byte sequences (not a plain ASCII sequence) and your browser has TextDecoder, then you should use that to decode your data (specify the required encoding for the TextDecoder):

var u8 = new Uint8Array([65, 66, 67, 68]);
var decoder = new TextDecoder('utf8');
var b64encoded = btoa(decoder.decode(u8));

If you need to support browsers that do not have TextDecoder (currently just IE and Edge), then the best option is to use a TextDecoder polyfill.

If your data contains plain ASCII (not multibyte Unicode/UTF-8) then there is a simple alternative using String.fromCharCode that should be fairly universally supported:

var ascii = new Uint8Array([65, 66, 67, 68]);
var b64encoded = btoa(String.fromCharCode.apply(null, ascii));

And to decode the base64 string back to a Uint8Array:

var u8_2 = new Uint8Array(atob(b64encoded).split("").map(function(c) {
    return c.charCodeAt(0); }));

If you have very large array buffers then the apply may fail with Maximum call stack size exceeded and you may need to chunk the buffer (based on the one posted by @RohitSengar). Again, note that this is only correct if your buffer only contains non-multibyte ASCII characters:

function Uint8ToString(u8a){
  var CHUNK_SZ = 0x8000;
  var c = [];
  for (var i=0; i < u8a.length; i+=CHUNK_SZ) {
    c.push(String.fromCharCode.apply(null, u8a.subarray(i, i+CHUNK_SZ)));
  }
  return c.join("");
}
// Usage
var u8 = new Uint8Array([65, 66, 67, 68]);
var b64encoded = btoa(Uint8ToString(u8));
Answer from kanaka on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › fromBase64
Uint8Array.fromBase64() - JavaScript - MDN Web Docs
A new Uint8Array object containing the decoded bytes from the base64-encoded string. ... Thrown if the input string contains characters outside the specified alphabet, or if the last chunk does not satisfy the lastChunkHandling option. ... The input string is not a string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › toBase64
Uint8Array.prototype.toBase64() - JavaScript - MDN Web Docs
This method should be preferred over Window.btoa(), especially if you already have a Uint8Array holding the object, because you don't need to convert it to a string first. ... An object customizing the base64 string format.
Top answer
1 of 16
161

If your data may contain multi-byte sequences (not a plain ASCII sequence) and your browser has TextDecoder, then you should use that to decode your data (specify the required encoding for the TextDecoder):

var u8 = new Uint8Array([65, 66, 67, 68]);
var decoder = new TextDecoder('utf8');
var b64encoded = btoa(decoder.decode(u8));

If you need to support browsers that do not have TextDecoder (currently just IE and Edge), then the best option is to use a TextDecoder polyfill.

If your data contains plain ASCII (not multibyte Unicode/UTF-8) then there is a simple alternative using String.fromCharCode that should be fairly universally supported:

var ascii = new Uint8Array([65, 66, 67, 68]);
var b64encoded = btoa(String.fromCharCode.apply(null, ascii));

And to decode the base64 string back to a Uint8Array:

var u8_2 = new Uint8Array(atob(b64encoded).split("").map(function(c) {
    return c.charCodeAt(0); }));

If you have very large array buffers then the apply may fail with Maximum call stack size exceeded and you may need to chunk the buffer (based on the one posted by @RohitSengar). Again, note that this is only correct if your buffer only contains non-multibyte ASCII characters:

function Uint8ToString(u8a){
  var CHUNK_SZ = 0x8000;
  var c = [];
  for (var i=0; i < u8a.length; i+=CHUNK_SZ) {
    c.push(String.fromCharCode.apply(null, u8a.subarray(i, i+CHUNK_SZ)));
  }
  return c.join("");
}
// Usage
var u8 = new Uint8Array([65, 66, 67, 68]);
var b64encoded = btoa(Uint8ToString(u8));
2 of 16
117

If you are using Node.js then you can use this code to convert Uint8Array to base64

var u8 = new Uint8Array([65, 66, 67, 68]);
var b64 = Buffer.from(u8).toString('base64');
🌐
GitHub
gist.github.com › av01d › e60f6edf3c5be6cd403eabb3d1f838f4
Convert base64 string to Uint8Array (one liner) · GitHub
Convert base64 string to Uint8Array (one liner) Raw · convertDataURItoBinary.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
npm
npmjs.com › package › base64-to-uint8array
base64-to-uint8array - npm
Convert a base64 string to a Uint8Array in Node and the browser · npm install base64-to-uint8array · var toUint8Array = require('base64-to-uint8array') var arr = toUint8Array('aGVsbG8gd29ybGQ=') console.log(arr) // the bytes for "hello world" ...
      » npm install base64-to-uint8array
    
Published: Jun 11, 2017
Version: 1.0.0
Author: Mathias Buus
🌐
GitHub
gist.github.com › borismus › 1032746
Convert a base64 string into a binary Uint8 Array · GitHub
A note on the one-liners: While Uint8Array.from(atob(base64), (v) => v.charCodeAt(0)); really looks nice, I ran into memory issues on Chrome when converting a 200MB binary while the for-loop worked flawlessly · Copy link · Copy Markdown · Uncaught DOMException: String contains an invalid character convertDataURIToBinary debugger eval code:6 downloadFile debugger eval code:17 debugger eval code:1 · Sign up for free to join this conversation on GitHub.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › setFromBase64
Uint8Array.prototype.setFromBase64() - JavaScript | MDN
Will never be greater than this Uint8Array's byteLength. ... Thrown if the input string contains characters outside the specified alphabet, or if the last chunk does not satisfy the lastChunkHandling option. ... The input string is not a string. The options object is not an object or undefined. The options are not of the expected values or undefined. This example uses the default alphabet and lastChunkHandling options to decode a base64 string into an existing Uint8Array.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › convert-base64-string-to-arraybuffer-in-javascript
Convert base64 String to ArrayBuffer In JavaScript - GeeksforGeeks
August 5, 2025 - function base64ToArrayBuffer(base64) { const binaryString = atob(base64); const length = binaryString.length; const bytes = new Uint8Array(length); for (let i = 0; i < length; i++) { bytes[i] = binaryString.charCodeAt(i); } return bytes.buffer; ...
🌐
CodeSandbox
codesandbox.io › examples › package › base64-to-uint8array
base64-to-uint8array examples - CodeSandbox
Use this online base64-to-uint8array playground to view and fork base64-to-uint8array example apps and templates on CodeSandbox.
Find elsewhere
🌐
TC39
tc39.es › proposal-arraybuffer-base64 › spec
Uint8Array to/from base64
October 28, 2025 - Let outAscii be the sequence of code points which results from encoding toEncode according to the base64url encoding specified in section 5 of RFC 4648.
🌐
Bun
bun.sh › utilities › encode and decode base64 data
Encode and decode base64 data - Bun
July 7, 2026 - const encoded = Buffer.from("hello world").toBase64(); // => "aGVsbG8gd29ybGQ=" const bytes = Buffer.from(encoded, "base64"); bytes instanceof Uint8Array; // => true const text = bytes.toString("utf8"); // => "hello world"
🌐
Blobfolio
blobfolio.com › 2019 › better-binary-batter-mixing-base64-and-uint8array
Better Binary Batter: Mixing Base64 and Uint8Array — Blobfolio
October 17, 2019 - Most front-end web developers facing such a situation would automatically reach for Base64, a common encoding scheme that plucks entries from a 64-letter alphabet to represent six bits of binary data. Support for Base64 encoding runs deep through the web technology ecosystem, but internally, most browsers natively store binary data in a special TypedArray consisting of unsigned 8-bit integers — a Uint8Array.
🌐
Webdevtutor
webdevtutor.net › blog › javascript-base64-to-uint8array
Encoding and Decoding Base64 Strings to Uint8Arrays in JavaScript
const base64String = 'SGVsbG8='; ... // SGVsbG8= To convert a Base64-encoded string to a Uint8Array, you can use the atob function and then create a new Uint8Array instance with the decoded data:...
🌐
stdlib
stdlib.io › docs › api › latest › @stdlib › string › base › base64-to-uint8array
base64-to-uint8array | string.base | stdlib
var string2buffer = require( '@stdlib/buffer/from-string' ); var str = string2buffer( 'Hello World!' ).toString( 'base64' ); // returns 'SGVsbG8gV29ybGQh' var out = base64ToUint8Array( str ); // returns <Uint8Array>[ 72, 101, ...
🌐
TC39
tc39.es › proposal-arraybuffer-base64
Proposed Support for Base64 in JavaScript
let arr = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]); console.log(arr.toHex()); // '48656c6c6f20576f726c64' ... let string = '48656c6c6f20576f726c64'; console.log(Uint8Array.fromHex(string)); // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]) The base64 methods take an optional options bag which allows specifying the alphabet as either "base64" (the default) or "base64url" (the URL-safe variant).
Author: robbie-cahill
🌐
GitHub
github.com › tc39 › proposal-arraybuffer-base64
GitHub - tc39/proposal-arraybuffer-base64: TC39 proposal for Uint8Array<->base64/hex · GitHub
JavaScript has Uint8Arrays to work with binary data, but no built-in mechanism to encode that data as base64, nor to take base64'd data and produce a corresponding Uint8Arrays. This is a proposal to fix that.
Author: tc39
🌐
Bun
bun.com › modules › globals › uint8arrayconstructor › frombase64
Uint8ArrayConstructor.fromBase64 method | globals module | Bun
May 21, 2026 - base64: string, offset?: number · ): { read: number; written: number }; Set the contents of the Uint8Array from a base64 encoded string · @param base64 · The base64 encoded string to decode into the array · @param offset · Optional starting ...