Skip to content

Instantly share code, notes, and snippets.

@gengue
Created August 11, 2022 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gengue/d1d299b5f5dba06ae9912b6b475b0ee2 to your computer and use it in GitHub Desktop.
Save gengue/d1d299b5f5dba06ae9912b6b475b0ee2 to your computer and use it in GitHub Desktop.
Encode and decode base64 with javascript
function encodeBase64(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
function decodeBase64(str) {
return decodeURIComponent(escape(window.atob(str)));
}
// Usage:
encodeBase64('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
decodeBase64('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment