Skip to content

Instantly share code, notes, and snippets.

@fundon
Forked from Hyvi/utf-8-base64.js
Created December 14, 2011 08:12
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 fundon/1475696 to your computer and use it in GitHub Desktop.
Save fundon/1475696 to your computer and use it in GitHub Desktop.
/**
*
Unicode Strings
In most browsers, calling window.btoa on a Unicode string will cause a Character Out Of Range exception.
To avoid this, consider this pattern, noted by Johan Sundström
(http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html):
*/
function utf8_to_b64( str ) {
return window.btoa(unescape(encodeURIComponent( str )));
}
function b64_to_utf8( str ) {
return decodeURIComponent(escape(window.atob( str )));
}
// Usage:
utf8_to_b64('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
b64_to_utf8('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment