简体   繁体   English

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

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

Suppose I have a base64 encoded string and I want to convert it into an ArrayBuffer , I can do it in this way:假设我有一个 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`  

From String.protoptype.charCodeAt() , it will return an integer between 0 and 65535 representing the UTF-16 code unit at the given index.String.protoptype.charCodeAt() ,它将返回一个介于 0 和 65535 之间的 integer,表示给定索引处的 UTF-16 代码单元。 But an Uint8Array 's range value is [0, 255].但是Uint8Array的范围值是 [0, 255]。

I was initially thinking that the code point we obtained from charCodeAt() could go out of the bound of the Uint8Array range.我最初认为我们从charCodeAt()获得的代码点可能超出Uint8Array范围的范围 go。 Then I checked the built-in atob() function, which returns an ASCII string containing decoded data.然后我检查了内置的atob() function,它返回一个包含解码数据的ASCII字符串。 According to Binary Array , ASCII string has a range from 0 to 127, which is included in the range of Uint8Array , and that's why we are safe to use charCodeAt() in this case.根据Binary Array ,ASCII 字符串的范围是 0 到 127,它包含在Uint8Array的范围内,这就是我们在这种情况下可以安全使用charCodeAt()的原因。

That's my understanding.这是我的理解。 I'm not sure if I interpret this correctly.我不确定我是否正确解释了这一点。 Thanks for your help!谢谢你的帮助!

So looks like my understanding is correct.所以看起来我的理解是正确的。

Thanks to @Konrad, and here is his/her add-up:感谢@Konrad,这是他/她的补充:

charCodeAt is designed to support utf-16. charCodeAt 旨在支持 utf-16。 And utf-16 was designed to be compatible with ASCII so the first 256 characters have exact values like in ASCII encoding. utf-16 被设计为与 ASCII 兼容,因此前 256 个字符具有与 ASCII 编码一样的精确值。

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

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