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 › setFromBase64
Uint8Array.prototype.setFromBase64() - JavaScript | MDN
A base64 string encoding bytes to write into a Uint8Array. It has the same requirements as the string parameter of Uint8Array.fromBase64().
Discussions

javascript - How to convert uint8 Array to base64 Encoded String? - Stack Overflow
I got a webSocket comunication, I recieve base64 encoded string, convert it to uint8 and work on it, but now I need to send back, I got the uint8 array, and need to convert it to base64 string, so I can send it. How can I make this conversion? ... MDN implementations for Uint8array/ArrayBuffer ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - How can I convert a base64 string to ArrayBuffer? - Stack Overflow
From MDN : Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The Uint8Array typed array represents an array of 8-bit unsigned integers, and we are working with ASCII representation of the data (which is also an 8-bit table).. 2016-06-17T12:57:34.657Z+00:00 ... This is not correct. It allows javascript ... More on stackoverflow.com
🌐 stackoverflow.com
Base64 to ArrayBuffer conversion is generating corrupted image file
This post helped me solving the problem. const imageBase64Str = req.body.logo.replace(/^.+,/, '') const buf = Buffer.from(imageBase64Str, 'base64') More on reddit.com
🌐 r/node
4
0
February 12, 2023
JS: How to convert ArrayBuffer to string?
Convert it to a Uint8Array, then convert that to a base64 string with btoa: https://stackoverflow.com/questions/9267899/arraybuffer-to-base64-encoded-string More on reddit.com
🌐 r/learnprogramming
1
1
May 2, 2023
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › toBase64
Uint8Array.prototype.toBase64() - JavaScript - MDN Web Docs
It can be one of the following: ... Encode input with the standard base64 alphabet, which uses + and /. ... Encode input with the URL-safe base64 alphabet, which uses - and _. ... A boolean specifying whether to omit padding characters (=) at the end of the base64 string.
🌐
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.
🌐
Webdevtutor
webdevtutor.net › blog › javascript-base64-to-uint8array
Encoding and Decoding Base64 Strings to Uint8Arrays in JavaScript
JavaScript provides two built-in functions for converting between text strings and binary data: atob (decode Base64) and btoa (encode Base64). These functions are part of the global window object, so you can use them directly in your code. ... const base64String = 'SGVsbG8='; // Hello! const ...
🌐
Jsben
jsben.ch › convert-base64-to-uint8array-flqmn
Convert Base64 to Uint8Array - JSBEN.CH JavaScript Benchmark
This test evaluates different methods to convert a base64-encoded string to a Uint8Array, which is a buffer-like object for storing bytes.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › convert-base64-string-to-arraybuffer-in-javascript
Convert base64 String to ArrayBuffer In JavaScript - GeeksforGeeks
August 5, 2025 - Below are the approaches to convert ... a Base64-encoded string into a binary string. The Uint8Array constructor can then be used to convert this binary string into an ArrayBuffer....
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');
Find elsewhere
🌐
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
function Uint8ArrayFromBase64Url(base64Url) { return Uint8Array.from(window.atob(base64Url.replace(/-/g, "+").replace(/_/g, "/")), (v) => v.charCodeAt(0)); } Copy link · Copy Markdown · ERROR: Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded I am running it with typescript react.
Author: gbhasha
🌐
JOYK
joyk.com › dig › detail › 1617379171538692
How to convert uint8 Array to base64 Encoded String? - JOYK Joy of Geek, Geek News, Link all geek
This function is needed because ... encoded string as value for applicationServerKey in pushManager.subscribe yet https://bugs.chromium.org/p/chromium/issues/detail?id=802280 · function urlBase64ToUint8Array(base64String) { var padding = '='.repeat((4 - base64String.length % 4) % 4); var base64 ...
🌐
Blobfolio
blobfolio.com › 2019 › better-binary-batter-mixing-base64-and-uint8array
Better Binary Batter: Mixing Base64 and Uint8Array in Static Javascript
October 17, 2019 - The first thing we'll need is a function to convert a Base64 string into a Uint8Array as that is not something Javascript can handle natively:
🌐
Runebook.dev
runebook.dev › en › docs › javascript › global_objects › uint8array › frombase64
Troubleshooting and Alternatives for JavaScript's Uint8Array.fromBase64
The most reliable alternative is to use the atob() (ASCII to Binary) function, which has been around for a long time. It decodes a Base64 string into a binary string, which you can then convert into a Uint8Array.
🌐
GitHub
github.com › MrPropre › base64-u8array-arraybuffer
GitHub - MrPropre/base64-u8array-arraybuffer: 📦 A simple, lightweight, and efficient JavaScript library to manage encoding/decoding between base64 data, Uint8Arrays, and ArrayBuffers
An example for this library would be to convert a base64url VAPID application server key into an Uint8Array to subscribe to Web Push Notifications. You can achieve this by using base64ToUint8Array(base64String) function.
Author: MrPropre
🌐
ThatSoftwareDude.com
thatsoftwaredude.com › content › 15283 › how-to-use-uint8array-base64-helpers-in-javascript
How to Use Uint8Array Base64 Helpers in JavaScript - ThatSoftwareDude.com
May 28, 2026 - JavaScript now has native `Uint8Array` helpers for converting bytes to and from Base64 and hex. Use them when your data is actually bytes: f...
🌐
GitHub
gist.github.com › enepomnyaschih › 54c437997f8202871278d0fdf68148ca
https://www.npmjs.com/package/byte-base64 - Encode JS Uint8Array, simple array of bytes or native JS string to base64 and back - TypeScript version · GitHub
https://www.npmjs.com/package/byte-base64 - Encode JS Uint8Array, simple array of bytes or native JS string to base64 and back - TypeScript version - base64.ts
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array
Uint8Array - JavaScript - MDN Web Docs
Uint8Array.fromBase64(), Uint8Array.prototype.toBase64(), and Uint8Array.prototype.setFromBase64() for working with base64 strings, where 3 bytes are encoded by 4 characters that are either 0–9, A–Z, a–z, "+", and "/" (or "-" and "_", if using URL-safe base64).