js base64解码方法 不用atob
时间: 2023-03-30 11:01:17 浏览: 137
JS Base64编码/解码
5星 · 资源好评率100%
可以使用以下代码进行 js base64 解码:
```
function base64Decode(str) {
var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var base64Map = {};
for (var i = ; i < base64Chars.length; i++) {
base64Map[base64Chars.charAt(i)] = i;
}
var binaryStr = "";
for (var i = ; i < str.length; i++) {
if (str.charAt(i) in base64Map) {
binaryStr += ("000000" + base64Map[str.charAt(i)].toString(2)).slice(-6);
}
}
var outputStr = "";
for (var i = ; i < binaryStr.length; i += 8) {
outputStr += String.fromCharCode(parseInt(binaryStr.substr(i, 8), 2));
}
return outputStr;
}
```
使用方法:
```
var encodedStr = "SGVsbG8gV29ybGQh";
var decodedStr = base64Decode(encodedStr);
console.log(decodedStr); // 输出 "Hello World!"
```
阅读全文