Codecs in JavaScript

<script>

//    for(let i = 0;i<10;i++){
//        console.log(i)
//    }
//    {
// var a = 9;
//    }
//    console.log(a);

//1
    //base64 encoding bWptYWFhYQ==
    console.log(window.btoa("mjmaaaa"));
    //base64 decode mjmaaaa
    console.log(window.atob("bWptYWFhYQ=="));
    //For those with Chinese characters, the above codec method cannot be used
//2
    let str = "I love China";
   // console.log(window.btoa(str));
    //Uncaught DOMException: Failed to execute 'btoa' on
    // 'Window': The string to be encoded contains characters outside of the Latin1 range.

    //window.encodeURIComponent和window.decodeURIComponent
    //coding
    console.log(window.btoa(window.encodeURIComponent(str)));//JUU2JTg4JTkxJUU3JTg4JUIxJUU0JUI4JUFEJUU1JTlCJUJE
    console.log(window.btoa(window.encodeURIComponent("mjmaaaa")));//bWptYWFhYQ== is consistent with the above results
    //decoding
    console.log(window.decodeURIComponent(window.atob("JUU2JTg4JTkxJUU3JTg4JUIxJUU0JUI4JUFEJUU1JTlCJUJE")));//I love China
//3
    

</script>

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326766511&siteId=291194637