function customAtob(base64) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
let bufferLength = base64.length * 0.75;
const len = base64.length;
let i;
let p = 0;
let encoded1, encoded2, encoded3, encoded4;if (base64[base64.length - 1] === "=") {
bufferLength--;
if (base64[base64.length - 2] === "=") {
bufferLength--;
}
}const arraybuffer = new ArrayBuffer(bufferLength);
const bytes = new Uint8Array(arraybuffer);for (i = 0; i < len; i += 4) {
encoded1 = chars.indexOf(base64[i]);
encoded2 = chars.indexOf(base64[i + 1]);
encoded3 = chars.indexOf(base64[i + 2]);
encoded4 = chars.indexOf(base64[i + 3]);bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}return arraybuffer;
}
自己实现的atob函数(兼容微信小程序)
于 2024-01-12 16:38:23 首次发布