假设我有一个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!
发布评论
评论(1)
因此,我的理解是正确的。
感谢@konrad,这是他/她的添加:
So looks like my understanding is correct.
Thanks to @Konrad, and here is his/her add-up: