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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步