charCodeAt(...)& 0xff完成了? [英] What does charCodeAt(...) & 0xff accomplish?
问题描述
我不确定0xFF在这里做了什么...
是否只是为了确保二进制代码长度为8位或与签名/无符号编码有关? ty。
i'm not sure what 0xFF does here... is it there just to make sure that the binary code is 8bit long or has something to do with the signed/unsigned encoding? ty.
var nBytes = data.length, ui8Data = new Uint8Array(nBytes);
for (var nIdx = 0; nIdx < nBytes; nIdx++) {
ui8Data[nIdx] = data.charCodeAt(nIdx) & 0xff;
}
XHR.send(ui8Data);
推荐答案
你的第一个猜测是对的。它只需要 data.charCodeAt 。
You're right with your first guess. It takes only the least significant 8 bits of what's returned by data.charCodeAt.
charCodeAt
将返回0..65536范围内的值。此代码将该范围截断为0..255。实际上,它将字符串中的每个16位字符,假设它可以适合8位,并丢弃高位字节。
charCodeAt
will return a value in the range of 0..65536. This code truncates that range to 0..255. Effectively, it's taking each 16-bit character in the string, assuming it can fit into 8 bits, and throwing out the upper byte.
这篇关于charCodeAt(...)& 0xff完成了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!