Skip to content

Instantly share code, notes, and snippets.

@rainyjune
Created July 20, 2019 08:18
Show Gist options
  • Save rainyjune/29535529807bed76c7a2815c0f07f58a to your computer and use it in GitHub Desktop.
Save rainyjune/29535529807bed76c7a2815c0f07f58a to your computer and use it in GitHub Desktop.
// ucs-2 string to base64 encoded ascii
function utoa(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
// base64 encoded ascii to ucs-2 string
function atou(str) {
return decodeURIComponent(escape(window.atob(str)));
}
// Usage:
utoa('✓ à la mode'); // 4pyTIMOgIGxhIG1vZGU=
atou('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
utoa('I \u2661 Unicode!'); // SSDimaEgVW5pY29kZSE=
atou('SSDimaEgVW5pY29kZSE='); // "I ♡ Unicode!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment