- Convert a TypedArray Buffer to Base64 in JavaScript
- from Uint8Array to Base64
- from Base64 to Uint8Array
- This needs to be standardized!
- Saved searches
- Use saved searches to filter your results more quickly
- gbhasha/base64-to-uint8array
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- base64 Uint8Array ArrayBuffer
- Installation
- CDN
- Local
- Available functions
- Useful case
- Tests
- Saved searches
- Use saved searches to filter your results more quickly
- License
- MrPropre/base64-u8array-arraybuffer
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
Convert a TypedArray Buffer to Base64 in JavaScript
This code was inspired by Unibabel JS, which in turn was inspired by MDN’s The Unicode Problem.
If you need a pure «vanilla» JavaScript implementation which does not rely on the DOM or node.js, check out beatgammit’s base64-js.
Note: You can get this to work all the way back to MSIE6 by replacing ‘Uint8Array()’ with ‘Array()’ and providing an Array polyfill for ‘map’ and ‘forEach’.
from Uint8Array to Base64
'use strict'; function bufferToBase64(buf) < var binstr = Array.prototype.map.call(buf, function (ch) < return String.fromCharCode(ch); >).join(''); return btoa(binstr); >
// "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=="
from Base64 to Uint8Array
'use strict'; function base64ToBuffer(base64) < var binstr = atob(base64); var buf = new Uint8Array(binstr.length); Array.prototype.forEach.call(binstr, function (ch, i) < buf[i] = ch.charCodeAt(0); >); return buf; >
var base64 = "SSDCvSDimaUg8J+SqQ=="; var buf = base64ToBuffer(base64); var arr = Array.prototype.slice.call(buf); // "I ½ ♥ 💩"; // [73, 32, 194, 189, 32, 226, 153, 165, 32, 240, 159, 146, 169]
This needs to be standardized!
Please contact your local JavaScript standardization committee to recommend that a feature be standardized in JavaScript.
Note: That atob and btoa are not even part of JavaScript, they are proprietary to the DOM.
Thanks! It’s really motivating to know that people like you are benefiting from what I’m doing and want more of it. 🙂
(you can learn about the bigger picture I’m working towards on my patreon page )
© AJ ONeal 2004-2019. Licensed CC-3.0. Compiled by Desi . Free SSL via Greenlock.js . Theme Cosmo
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A Javascript utility function which converts the base 64 safe encoded string to UInt8Array.
gbhasha/base64-to-uint8array
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
The Uint8Array typed array represents an array of 8-bit unsigned integers. The contents are initialized to 0. Once established, you can reference elements in the array using the object’s methods, or using standard array index syntax (that is, using bracket notation).
Used while exchanging secret key between client and server.
npm install urlb64touint8array
import toUint8Array from 'urlb64touint8array';
var toUint8Array = require('urlb64touint8array');
About
A Javascript utility function which converts the base 64 safe encoded string to UInt8Array.
base64 Uint8Array ArrayBuffer
📦 A simple, lightweight, and efficient JavaScript library to manage encoding and decoding between base64 data, Uint8Arrays, and ArrayBuffers. This library perfectly works with Node.js and the browser.
Installation
CDN
The easiest way to use base64-u8array-arraybuffer is to include the library from a CDN:
script src pl-s">https://cdn.jsdelivr.net/npm/base64-u8array-arraybuffer@1.0.3/dist/base64-u8array-arraybuffer.min.js">script>
Then, in your JavaScript code:
// Unpacking needed functions from the global object const base64ToArrayBuffer > = base64u8ArrayBuffer const buffer = base64ToArrayBuffer('base64 string here')
Local
You can also install base64-u8array-arraybuffer in your project.
npm i base64-u8array-arraybuffer
And then, you can import the library as ES Module:
import base64ToArrayBuffer > from 'base64-u8array-arraybuffer' const buffer = base64ToArrayBuffer('base64 string here')
You can also use commonJS syntax with require()
Note ES Module syntax also works in modern browsers. You just need to add type=»module» to your tag.
script type pl-s">module" src pl-s">. ">script>
Available functions
Function name | Description |
---|---|
base64ToUint8Array(base64String) | base64 to Uint8Array |
uint8ArrayToBase64(uint8Array) | Uint8Array to Base64 (Works with any TypedArray. You can use typedArrayToBase64() alias.) |
uint8ArrayToArrayBuffer(uint8Array) | Uint8Array to ArrayBuffer (Works with any TypedArray. You can use typedArrayToArrayBuffer() alias.) |
arrayBufferToUint8Array(arrayBuffer) | ArrayBuffer to Uint8Array |
base64ToArrayBuffer(base64String) | base64 to ArrayBuffer |
arrayBufferToBase64(arrayBuffer) | ArrayBuffer to base64 |
Useful case
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.
const base64ToUint8Array > = base64u8ArrayBuffer const applicationServerPublicKey = 'base64url public key' async function subscribeUserToPush(registration) // . const subscription = await registration.pushManager.subscribe( userVisibleOnly: true, applicationServerKey: base64ToUint8Array(applicationServerPublicKey) >) // . >
Tests
There are no tests for the moment.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
📦 A simple, lightweight, and efficient JavaScript library to manage encoding/decoding between base64 data, Uint8Arrays, and ArrayBuffers
License
MrPropre/base64-u8array-arraybuffer
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
base64 Uint8Array ArrayBuffer
📦 A simple, lightweight, and efficient JavaScript library to manage encoding and decoding between base64 data, Uint8Arrays, and ArrayBuffers. This library perfectly works with Node.js and the browser.
The easiest way to use base64-u8array-arraybuffer is to include the library from a CDN:
script src pl-s">https://cdn.jsdelivr.net/npm/base64-u8array-arraybuffer@1.0.3/dist/base64-u8array-arraybuffer.min.js">script>
Then, in your JavaScript code:
// Unpacking needed functions from the global object const base64ToArrayBuffer > = base64u8ArrayBuffer const buffer = base64ToArrayBuffer('base64 string here')
You can also install base64-u8array-arraybuffer in your project.
npm i base64-u8array-arraybuffer
And then, you can import the library as ES Module:
import base64ToArrayBuffer > from 'base64-u8array-arraybuffer' const buffer = base64ToArrayBuffer('base64 string here')
You can also use commonJS syntax with require()
Note ES Module syntax also works in modern browsers. You just need to add type=»module» to your tag.
script type pl-s">module" src pl-s">. ">script>
Function name | Description |
---|---|
base64ToUint8Array(base64String) | base64 to Uint8Array |
uint8ArrayToBase64(uint8Array) | Uint8Array to Base64 (Works with any TypedArray. You can use typedArrayToBase64() alias.) |
uint8ArrayToArrayBuffer(uint8Array) | Uint8Array to ArrayBuffer (Works with any TypedArray. You can use typedArrayToArrayBuffer() alias.) |
arrayBufferToUint8Array(arrayBuffer) | ArrayBuffer to Uint8Array |
base64ToArrayBuffer(base64String) | base64 to ArrayBuffer |
arrayBufferToBase64(arrayBuffer) | ArrayBuffer to base64 |
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.
const base64ToUint8Array > = base64u8ArrayBuffer const applicationServerPublicKey = 'base64url public key' async function subscribeUserToPush(registration) // . const subscription = await registration.pushManager.subscribe( userVisibleOnly: true, applicationServerKey: base64ToUint8Array(applicationServerPublicKey) >) // . >
There are no tests for the moment.