为什么string.protoptype.charcodeat()可以将二进制字符串转换为uint8array?

发布于 2025-01-27 14:55:50 字数 1156 浏览 2 评论 0 原文

假设我有一个base64编码的字符串,我想将其转换为ArrayBuffer, I can do it in this way:

// 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

最初,我认为我们从 charcodeat()获得的代码点可以脱离 uint8array 范围的界限。然后,我检查了https://developer.mozilla.org/en-us/docs/web/api/atob/Atob“ rel =“ nofollow noreferrer”> atob()它返回包含解码数据的ASCII字符串。根据二进制阵列从0到127,其中包括 uint8array 的范围,这就是为什么我们可以安全地使用 charcodeat()在这种情况下。

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

Suppose I have a base64 encoded string and I want to convert it into an ArrayBuffer, I can do it in this way:

// 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. But an Uint8Array's range value is [0, 255].

I was initially thinking that the code point we obtained from charCodeAt() could go out of the bound of the Uint8Array range. Then I checked the built-in atob() function, which returns an ASCII string containing decoded data. 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.

That's my understanding. I'm not sure if I interpret this correctly. Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2025-02-03 14:55:50

因此,我的理解是正确的。

感谢@konrad,这是他/她的添加:

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

So looks like my understanding is correct.

Thanks to @Konrad, and here is his/her add-up:

charCodeAt is designed to support utf-16. And utf-16 was designed to be compatible with ASCII so the first 256 characters have exact values like in ASCII encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文