Using Javascript's atob to decode base64 doesn't properly decode utf-8 strings
Javascript's atob can properly decode base64 strings that have been encoded with ASCII characters, for example, window.atob('YQ==') will return the ASCII character 'a'. However, it won't properly decode base64 strings that have been encoded with UTF-8 Unicode characters, for example, window.atob('4pyTIMOgIGxhIG1vZGU=') will return '⢠à la mode' instead of '✓ à la mode'.
To properly decode a base64 string that has been encoded with UTF-8, we need to use the escape and unescape functions. In this case, window.atob(unescape(encodeURIComponent('✓ à la mode'))) will return '4pyTIMOgIGxhIG1vZGU=' and window.atob('4pyTIMOgIGxhIG1vZGU=') will return '✓ à la mode'.
Another option to handle the incoming base64-encoded stream so that it's decoded as utf-8 is to use the TextDecoder class. This class provides a way to decode a base64-encoded string into a UTF-8 string. Here's an example of how to use it:
const text = '4pyTIMOgIGxhIG1vZGU=';
const decoder = new TextDecoder('utf-8');
const decodedText = decoder.decode(Uint8Array.from(atob(text)));
console.log(decodedText); // '✓ à la mode'
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3