[Home] > Snippets  > Languages  > JavaScript  > Strings  >  Convert an uint8 array to a base64 encoded string

Convert an uint8 array to a base64 encoded string

JavaScript

const uint8ToBase64 = (arr) =>
btoa(Array(arr.length)
.fill('')
.map((_, i) => String.fromCharCode(arr[i]))
.join(''))

// For Node.js
const uint8ToBase64 = (arr) => Buffer.from(arr).toString('base64')

TypeScript

const uint8ToBase64 = (arr: Uint8Array): string =>
btoa(Array(arr.length)
.fill('')
.map((_, i) => String.fromCharCode(arr[i]))
.join(''))

// For Node.js
const uint8ToBase64 = (arr: Uint8Array): string => Buffer.from(arr).toString('base64')

See also