Skip to content

Instantly share code, notes, and snippets.

Created May 14, 2014 01:47
function utf8_to_b64( str ) {
return window.btoa(encodeURIComponent( escape( str )));
}
function b64_to_utf8( str ) {
return unescape(decodeURIComponent(window.atob( str )));
}
// Usage:
utf8_to_b64('✓ à la mode'); // JTI1dTI3MTMlMjUyMCUyNUUwJTI1MjBsYSUyNTIwbW9kZQ==
b64_to_utf8('JTI1dTI3MTMlMjUyMCUyNUUwJTI1MjBsYSUyNTIwbW9kZQ=='); // "✓ à la mode"
utf8_to_b64('I \u2661 Unicode!'); // SSUyNTIwJTI1dTI2NjElMjUyMFVuaWNvZGUlMjUyMQ==
b64_to_utf8('SSUyNTIwJTI1dTI2NjElMjUyMFVuaWNvZGUlMjUyMQ=='); // "I ♡ Unicode!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment