JSでのBase64エンコードとデコード(中国語の文字化けを解決)

通常、バックエンドによって返された暗号化された文字列をデコードするために使用されます。

ここに画像の説明を挿入します

/**
  * 编码base64
  */
 function Encode64(str) {
    
    
     return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
         function toSolidBytes(match, p1) {
    
    
             return String.fromCharCode('0x' + p1);
         }));
 }
 /**
  * 解码base64
  */
 function Decode64(str) {
    
    
     return decodeURIComponent(atob(str).split('').map(function (c) {
    
    
         return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
     }).join(''));
 }

 let Value = '2022-11-12'
 let encodeValue
 let decodeValue
 encodeValue = Encode64(Value) // 编码base64
 decodeValue = Decode64(encodeValue) // 解码base64
 console.log('Value:', Value);
 console.log('encodeValue:', encodeValue);
 console.log('decodeValue:', decodeValue);

おすすめ

転載: blog.csdn.net/qq_15957557/article/details/127819292