Base64編碼與解碼程式碼例項

螞蟻小編發表於2017-04-14

本章節介紹一下如何實現Base64編碼與解碼。

程式碼例項如下:

[JavaScript] 純文字檢視 複製程式碼
String.prototype.encodeBase64 = function(){
  return window.btoa(unescape(encodeURIComponent(this)));
}
String.prototype.decodeBase64 = function(){
  return decodeURIComponent(escape(window.atob(this)));
};
var str = "螞蟻部落";
var str_encode = str.encodeBase64();
var str_decode = str_encode.decodeBase64();
console.log(str_encode);
console.log(str_decode);

相關文章