Home > Article > Web Front-end > Why does Javascript\'s `atob` fail to decode UTF-8 base64 strings properly?
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:
<code class="javascript">const text = '4pyTIMOgIGxhIG1vZGU='; const decoder = new TextDecoder('utf-8'); const decodedText = decoder.decode(Uint8Array.from(atob(text))); console.log(decodedText); // '✓ à la mode'</code>
The above is the detailed content of Why does Javascript\'s `atob` fail to decode UTF-8 base64 strings properly?. For more information, please follow other related articles on the PHP Chinese website!