Base64 Encoding of utf-8 encoded string using Javascript

Posted on 13-03-2017 by Nadir Latif


Source: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_.22Unicode_Problem.22

The btoa function encodes a string using Base64 encoding scheme. If the string is utf-8 encoded then the btoa function will not work correctly. The solution is to first url encode the string then call window.btoa function.

To url encode a string we can use the encodeURIComponent function. to url decode a string we can use the decodeURIComponent function