10

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);
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
Ray
  • 103
  • 1
  • 4
  • 0xFF is hex for 255. It's masking the `charCodeAt(nIdx)` with 255 as a way of "selecting" the lower 8 bits. – vcsjones Dec 23 '13 at 19:30
  • There's more to this question than the title implies. The "duplicate" question has nothing to do with bitmasking. – Scott Mermelstein Dec 23 '13 at 19:45
  • It may be a duplicate of this question, though: http://stackoverflow.com/questions/20486551/javascript-function-to-convert-utf8-string/20487371#20487371 – Scott Mermelstein Dec 23 '13 at 19:51
  • @ScottMermelstein If there is more to it, help us out by editing the title to go over exactly what the OP is asking? Based on all the avialable text in the question; the questions appear to be duplicates. If they aren't, the best thing to do would be to edit the question to show those differences so it can be re-opened and the question he actually wants to be answered can be answered. – George Stocker Dec 23 '13 at 20:02
  • @GeorgeStocker Really? You don't see a difference between "What does 0X mean? I saw it mentioned in class" and someone posting code using "& 0xff" asking what it does? Hang on, I'll edit the title. – Scott Mermelstein Dec 23 '13 at 20:05

1 Answers1

10

You're right with your first guess. It takes only the least significant 8 bits of what's returned by data.charCodeAt.

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.

[6 years later edit] In the comments, we discovered a few things: you're questioning the code for the MDN polyfill for sendAsBinary. As you came to understand, the least significant byte does come first in little-endian systems, while the most significant byte comes first in big-endian systems. Given that this is code from MDN, the code certainly does what was intended - by using FileReader.readAsBinaryString, it stores 8bit values into a 16bit holder. If you're worried about data loss, you can tweak the polyfill to extract the other byte using sData.charCodeAt(nIdx) && 0xff00 >> 8.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • ok.. what i really dont understand here.. if data contains data from a file, so i dont know what the char will look like, which part will he keep? -- if the data looks like 01010101 11110000 -- when ANDing with 255 what will remain? maybe i dont understand what the least significant bit is.. – Ray Dec 23 '13 at 20:45
  • @Ray It's [easy enough](http://jsfiddle.net/92g9X/) to test that question. If your byte is 0x99f0, `0x99f0 & 0x00ff` (same as `0x99f0 & 0xff`) gives 0xf0. If you're reading a file, I assume you'd have to take care of endianess of the file yourself. If you know it to be little endian, you'd have to manually account for it yourself, with `0x99f0 & 0xff00 >> 8`. Check the question I linked to for a good way to confirm the the unicode value is in the appropriate range. – Scott Mermelstein Dec 23 '13 at 20:58
  • 1
    ok..i think i got it.. endianness was something i did not understood.. the least significant byte comes first in memory in little endian.. this is the sendAsBinary function and if what i understand is right, it wont work in big endian systems if char takes more than one byte.. ty very much.. – Ray Dec 23 '13 at 22:48
  • @Ray Wow. It would've helped a ton if you referred to [the MDN polyfill for sendAsBinary](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#sendAsBinary()) in your question. Two quick things - they make careful use of `FileReader.readAsBinaryString`, so unless MDN has messed up, it's likely ok - it reads the 8bit values into a 16bit holder. Also, if you're worried about data loss, you can tweak the polyfill to extract the other byte using `sData.charCodeAt(nIdx) && 0xff00 >> 8` – Scott Mermelstein Dec 23 '13 at 23:05
  • @ScottMermelstein this is an old question/answer (in fact, I don't know how I stumbled here), but these last two comments (Ray's clarification and your expansion on sendAsBinary) seem worthy of incorporating into your answer. – vol7ron Aug 29 '19 at 04:04