I ended up using canvas.toDataURL instead of trying to convert this myself.

Answer from Aseliot on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 27707528 › how-to-convert-uint8clampedarray-of-webp-image-picked-from-canvas-to-base64
How to convert Uint8ClampedArray of webp image picked from canvas to base64
December 30, 2014 - function Uint8toBase64(idata) { this.idata = idata; var u = new Uint8ClampedArray(4); for (var i = 0; i < idata.length; i += 4) { idata[0] = u[i]; idata[1] = u[i + 1]; idata[2] = u[i + 2]; idata[3] = u[i + 3]; } var str = String.fromCharCod...
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');
🌐
CodePen
codepen.io › ydaniv › pen › JjddxPG
ArrayBuffer to base64
const source = document.getElementById('source') const img = document.getElementById('photo') const ctx = source.getContext('2d') function load () { ctx.drawImage(this, 0, 0, img.naturalWidth, img.naturalHeight, 0, 0, 100, 100) const data = ctx.getImageData(0, 0, 100, 100) const target = document.getElementById('target') const tctx = target.getContext('2d') const buffer = data.data.buffer /* Host-land code starts here */ const array = new Uint8ClampedArray([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]) const imgData = new ImageData(array, 5, 1) target.width = 5 target.height = 1 tctx.putImageData(imgData, 0, 0) console.log(tctx.canvas.toDataURL()) } img.onload = load;
🌐
Coolaj86
coolaj86.com › articles › typedarray-buffer-to-base64-in-javascript
Convert a TypedArray Buffer to Base64 in JavaScript
June 26, 2015 - // "I ½ ♥ 💩"; var arr = [73, 32, 194, 189, 32, 226, 153, 165, 32, 240, 159, 146, 169]; var data = new Uint8Array(arr); var base64 = bufferToBase64(data); // "SSDCvSDimaUg8J+SqQ=="
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › fromBase64
Uint8Array.fromBase64() - JavaScript - MDN Web Docs
The string must only contain characters in the base64 alphabet, which includes A–Z, a–z, 0–9, and two special characters, which are either + and / (if using alphabet: "base64" in options) or - and _ (if using alphabet: "base64url" in options). It may have padding = characters at the end.
Find elsewhere
🌐
Medium
medium.com › @j622amilah › javascript-basics-6-4b681b792298
JavaScript Basics 6. A list of conversion methods for… | by Practicing DatScy | Medium
November 22, 2024 - async function convert_canvas_to_base64str(ctx, canvasElement) { // ----------------------------- // Get canvas image const uint8ClampedArray = await ctx.getImageData(0, 0, canvasElement.width, canvasElement.height); // TypedArray Uint8ClampedArray // console.log('uint8ClampedArray: ', uint8ClampedArray); // Convert TypedArray to normalArray const normalArray = await Array.from(uint8ClampedArray.data); // console.log('normalArray: ', normalArray); // Convert [normalArray of bytes] to text_characters var ASCII_stringArray = normalArray.map((val) => { return String.fromCharCode(val); }); // cons
🌐
npm
npmjs.com › package › uint8-to-base64
uint8-to-base64 - npm
June 23, 2025 - import {encode, decode} from 'uint8-to-base64'; // const {encode, decode} = require('uint8-to-base64'); const utf8Binary = new Uint8Array(anyArrayBuffer); // encode converts Uint8Array instances to base64 strings const encoded = encode(utf8Binary); // it's just like any other string console.log(encoded); // decode converts base64 strings, encoded via this module, // into their original Uint8Array representation const decoded = decode(encoded); console.assert( JSON.stringify([...utf8Binary]) === JSON.stringify([...decoded]), 'safe Uint8Array to utf-16 conversion' );
      » npm install uint8-to-base64
    
Published: Jun 23, 2025
Version: 0.2.1
🌐
JavaScripture
javascripture.com › Uint8ClampedArray
Uint8ClampedArray JavaScript API
Gets and sets the element in this at index. index should be between 0 and this.length - 1. Setting an element to a value less than 0 will store 0 and greater than 255 will store 255 in the array. ... Returns the underlying buffer for this Uint8ClampedArray.
🌐
GitHub
github.com › WebReflection › uint8-to-base64
GitHub - WebReflection/uint8-to-base64: A safe Uint8Array to base64 string converter · GitHub
import {encode, decode} from 'uint8-to-base64'; // const {encode, decode} = require('uint8-to-base64'); const utf8Binary = new Uint8Array(anyArrayBuffer); // encode converts Uint8Array instances to base64 strings const encoded = encode(utf8Binary); // it's just like any other string console.log(encoded); // decode converts base64 strings, encoded via this module, // into their original Uint8Array representation const decoded = decode(encoded); console.assert( JSON.stringify([...utf8Binary]) === JSON.stringify([...decoded]), 'safe Uint8Array to utf-16 conversion' );
Author: WebReflection
🌐
Medium
medium.com › @koteswar.meesala › convert-array-buffer-to-base64-string-to-display-images-in-angular-7-4c443db242cd
Convert Array buffer to base64 string to display images in Angular 7 | by Kotesh Meesala | Medium
June 9, 2019 - You may need to make changes to the data using some standard JavaScript built-in methods to convert the ArrayBuffer to base64 string and make use of DomSanitizerlink API in angular to bypass the Angular’s built-in sanitization.
🌐
GitHub
gist.github.com › borismus › 1032746
Convert a base64 string into a binary Uint8 Array - Gist - GitHub
My question is, where do you call PDFJS.getDocument(array) to display the pdf in the library? I'm just a little lost in the implementation if someone could clarify it for me a little bit I would appreciate it, thanks. ... Thank you, @borismus! ... function Uint8ArrayFromBase64(base64) { return Uint8Array.from(window.atob(base64), (v) => v.charCodeAt(0)); }
🌐
GitHub
gist.github.com › jonleighton › 958841
Encode an ArrayBuffer as a base64 string · GitHub
While this thread was about "Converts an ArrayBuffer directly to base64, without any intermediate 'convert to string then use window.btoa' step" so I'm going a bit off topic, but there is a way to avoid the "larger than 30k bytes" issues using reduce.
🌐
GitHub
gist.github.com › paulownia › 86a1692e873d335dbb89e8bc55c2e74a
base64 encode/decode with Uint8Array on browser · GitHub
base64 encode/decode with Uint8Array on browser. GitHub Gist: instantly share code, notes, and snippets.
🌐
Cloudflare Community
community.cloudflare.com › developers › cloudflare workers
Uint8Array to Base64 in Workers - Cloudflare Workers - Cloudflare Community
March 29, 2022 - I am using some of the web crypto “techniques” to encrypt data I send to the client… so I can store it in a cache and use it on subsequent requests. And for added security. I have my encrypted data… but it’s an ArrayBuffer… Uint8Array. I’m struggling to convert it to base64 so ...
🌐
Blobfolio
blobfolio.com › 2019 › better-binary-batter-mixing-base64-and-uint8array
Better Binary Batter: Mixing Base64 and Uint8Array — Blobfolio
October 17, 2019 - In this case, the exact penalty will vary, because unlike Base64 which always uses one letter to represent six bits, Uint8Arrays use numbers of variable length to represent whole bytes. A small number, like "5", only requires one byte of storage to represent one byte of information.