I’m trying to use XMLHttpRequest to download binary data. When I set the charset to ‘x-user-defined’ so the browser won’t mess with the data, the browser does not set each byte of the response to the low byte of a UTF-16 string; instead, some bytes are prefixed by 0xf7. I am specifically trying to do this in an Android WebView, but I believe Firefox does the same thing.
Why is this happening? I’m getting exactly the data I want, but I have to && 0xff against each byte to strip away the unwanted 0xf7.
This solution is mentioned in Mozilla docs, and the explanation comes from Marcus Granado (text no longer available, but read the backup from web.archive.org):
When you do
& 0xffyou simply discard the byte in the high-order position.