base64 web前端js解码与转码

<script type="text/javascript">
function b64EncodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode('0x' + p1);
    }));
}

console.log(b64EncodeUnicode('我是很厉害的')); // "5oiR5piv5b6I5Y6J5a6z55qE"
function b64DecodeUnicode(str) {
    return decodeURIComponent(atob(str).split('').map(function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}

console.log(b64DecodeUnicode('MjA5OTEwNTM5')); // "我是很厉害的"

</script>

 

posted @ 2021-02-10 23:18  你好柴九  阅读(265)  评论(0编辑  收藏  举报