Skip to content
Advertisement

Why won’t window.btoa work on – ” characters in Javascript?

So I’m converting a string to BASE64 as shown in the code below…

var str = "Hello World";
var enc = window.btoa(str);

This yields SGVsbG8gV29ybGQ=. However if I add these characters – ” such as the code shown below, the conversion doesn’t happen. What is the reason behind this? Thank you so much.

var str = "Hello – World”";
var enc = window.btoa(str);

Advertisement

Answer

btoa is an exotic function in that it requires a “Binary String”, which is an 8-bit clean string format. It doesn’t work with unicode values above charcode 255, such as used by your em dash and “fancy” quote symbol.

You’ll either have to turn your string into a new string that conforms to single byte packing (and then manually reconstitute the result of the associated atob), or you can uri encode the data first, making it safe:

> var str = `Hello – World`;
> window.btoa(encodeURIComponent(str));
"SGVsbG8lMjAlRTIlODAlOTMlMjBXb3JsZA=="

And then remember to decode it again when unpacking:

> var base64= "SGVsbG8lMjAlRTIlODAlOTMlMjBXb3JsZA==";
> decodeURIComponent(window.atob(base64));
"Hello – World"
Advertisement