繁体   English   中英

为什么 String.protoptype.charCodeAt() 可以将二进制字符串转换为 Uint8Array?

[英]Why String.protoptype.charCodeAt() can convert binary string into an Uint8Array?

假设我有一个 base64 编码的字符串,我想将它转换成一个ArrayBuffer ,我可以这样做:

// base64 decode the string to get the binary data
const binaryString = window.atob(base64EncodedString);

// convert from a binary string to an ArrayBuffer
const buf = new ArrayBuffer(binaryString.length);
const bufView = new Uint8Array(buf);   
for (let i = 0, strLen = binaryString.length; i < strLen; i++) {
    bufView[i] = binaryString.charCodeAt(i);
}

// get ArrayBuffer: `buf`  

String.protoptype.charCodeAt() ,它将返回一个介于 0 和 65535 之间的 integer,表示给定索引处的 UTF-16 代码单元。 但是Uint8Array的范围值是 [0, 255]。

我最初认为我们从charCodeAt()获得的代码点可能超出Uint8Array范围的范围 go。 然后我检查了内置的atob() function,它返回一个包含解码数据的ASCII字符串。 根据Binary Array ,ASCII 字符串的范围是 0 到 127,它包含在Uint8Array的范围内,这就是我们在这种情况下可以安全使用charCodeAt()的原因。

这是我的理解。 我不确定我是否正确解释了这一点。 谢谢你的帮助!

所以看起来我的理解是正确的。

感谢@Konrad,这是他/她的补充:

charCodeAt 旨在支持 utf-16。 utf-16 被设计为与 ASCII 兼容,因此前 256 个字符具有与 ASCII 编码一样的精确值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM