js支持的編碼轉換方法【轉】


var encodedData = btoa('Hello, world'); // encode a string
var decodedData = atob(encodedData); // decode the string
function utoa(str) {
    return window.btoa(unescape(encodeURIComponent(str)));
}
// base64 encoded ascii to ucs-2 string
function atou(str) {
    return decodeURIComponent(escape(window.atob(str)));
}
// 使用:
utoa('✓ à la mode'); // 4pyTIMOgIGxhIG1vZGU=
atou('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"

utoa('I \u2661 Unicode!'); // SSDimaEgVW5pY29kZSE=
atou('SSDimaEgVW5pY29kZSE='); // "I ♡ Unicode!"

總結一下
btoa 將普通字符串轉化為base64編碼
atou base64編碼轉化為普通字符串
escape 可對字符串進行編碼
unescape 可對字符串進行解碼
encodeURIComponent(URIstring) 把字符串作為URI組件進行編碼。
URIstring 必需。一個字符串,含有URI組件或其他要編碼的文本。
decodeURIComponent 把字符串作為URI組件進行解碼。
URIstring 必需。一個字符串,含有URI組件或其他要編碼的文本。
注:關於URL和URI的區別可以看看這個
 
來源:https://blog.csdn.net/momDIY/article/details/78329718


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM