实现代码如下

// 字符串转base64
  getEncode64(str){   
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
        function toSolidBytes(match, p1) {
            return String.fromCharCode('0x' + p1);
      }));     
  }

  // base64转字符串(含中文)
  getDecode(str){
    return decodeURIComponent(atob(str).split('').map(function (c) {
          return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
      }).join(''));
  }
posted on 2022-10-08 10:58  奇迹般若果  阅读(784)  评论(0编辑  收藏  举报