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

原文地址:https://www.cnblogs.com/KillBugMe/p/13261620.html