Uint8Array.from和TextDecoder兼容低浏览器

const decodedData = decodeURIComponent(data)
const inputString = atob(decodedData);

1、Uint8Array.from兼容低浏览器
 const decryptedBytes = Uint8Array.from(inputString, c => c.charCodeAt(0)).map(byte => byte ^ 5)
对应的es5写法:
//
创建一个新的 Uint8Array 并分配足够的空间来存储每个字符的 Unicode 编码
var decodedBytes = new Uint8Array(inputString.length);
var decryptedBytes = [];
// 遍历字符串并将每个字符的 charCodeAt 结果写入到 Uint8Array 中
for (var i = 0; i < inputString.length; i++) {
  decodedBytes[i] = inputString.charCodeAt(i);
  decryptedBytes.push(decodedBytes[i] ^ 5);
}
2、
TextDecoder兼容低浏览器
  new TextDecoder().decode(decryptedBytes)
 对应的es5写法:
  decodeURIComponent(escape(String.fromCharCode.apply(null, decryptedBytes)))
 

 

posted @ 2024-03-23 15:38  chicidol  阅读(4)  评论(0)    收藏  举报