JavaScript uses Btoa and Atob for Base64 transcoding and decoding

Source: Internet
Author: User
Tags base64

How to use Base64 transcoding in JavaScript

Let str = ' javascript '; Let btoastr = Window.btoa (str);  transcoding results amf2yxnjcmlwda== Console.log (BTOASTR); Console.log (Window.atob (BTOASTR)); Decoding results JavaScript

The Base64 transcoding object can only be a string,

var str = "China, Chinese"; Window.btoa (str);

Error uncaught domexception:failed to execute ' btoa ' in ' Window ': the string to be encoded contains characters outside of The Latin1 range.

So how do we get him to support Chinese characters?

Let str = ' javascript, blog '; let btoastr = Window.btoa (window.encodeuricomponent (str));  transcoding results  Amf2yxnjcmlwdcuyqyvfnsu4rcu5qsvfnsvbrsvbmg==console.log (BTOASTR);
Console.log (Window.decodeuricomponent (Window.atob (BTOASTR))); Decoding results JavaScript, Blogs

This is the simple and practical btoa and Atob, I hope to be helpful to everyone.

JavaScript uses Btoa and Atob for Base64 transcoding and decoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.