Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript convert UTF-8 to base64

When I try to run the JS script below get an exception in the code:

var encode = window.btoa('Hiroto √')

"@lehesamudi.js:14:26
b.render/<@https://static.jsbin.com/js/prod/runner-4.1.8.min.js:1:13924
a.use/<@https://static.jsbin.com/js/prod/runner-4.1.8.min.js:1:10866
"

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try to escape before, please find the code below.

function encode(str) {
    return window.btoa(unescape(encodeURIComponent(str)));
}

function decode(str) {
    return decodeURIComponent(escape(window.atob(str)));
}

// Usage:
encode('Encode me with UTF-8 char: ∑ßåœ ≈ ∆c') // "RW5jb2RlIG1lIHdpdGggVVRGLTggY2hhcjog4oiRw5/DpcWTIOKJiCDiiIZj"
decode("RW5jb2RlIG1lIHdpdGggVVRGLTggY2hhcjog4oiRw5/DpcWTIOKJiCDiiIZj") //'Encode me with UTF-8 char: ∑ßåœ ≈ ∆c'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading