Home > Web Front-end > JS Tutorial > body text

Why does Javascript\'s `atob` fail to decode UTF-8 base64 strings properly?

DDD
Release: 2024-11-01 23:10:29
Original
378 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!