https://github.com/WebReflection/uint8-to-base64
A safe Uint8Array to base64 string converter
https://github.com/WebReflection/uint8-to-base64
Last synced: 8 months ago
JSON representation
A safe Uint8Array to base64 string converter
- Host: GitHub
- URL: https://github.com/WebReflection/uint8-to-base64
- Owner: WebReflection
- License: isc
- Created: 2020-02-18T12:24:56.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-08-17T08:17:55.000Z (over 3 years ago)
- Last Synced: 2025-04-13T23:58:09.841Z (9 months ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 30
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - uint8-to-base64
README
# A safe Uint8Array to base64 string converter
[](https://travis-ci.com/WebReflection/uint8-to-base64) [](https://coveralls.io/github/WebReflection/uint8-to-base64?branch=master)
**Social Media Photo by [Suzanne D. Williams](https://unsplash.com/@scw1217) on [Unsplash](https://unsplash.com/)**
Compatible with any binary data and every modern JS engine.
```js
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'
);
```
**Please note** this module requires global `atob` and `btoa` in NodeJS **< 16**, polyfilled in old tests in here as such.
```js
global.btoa = str => Buffer.from(str).toString('base64');
global.atob = str => Buffer.from(str, 'base64').toString();
```
Looking for a drop in module that converts into utf-16 strings instead? Check [uint8-to-utf16](https://github.com/WebReflection/uint8-to-utf16#readme) out!