Why are some bytes prefixed with 0xf7 when using charset: x-user-defined with XMLHttpRequest?
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):
The charset x-user-defined uses the UNICODE Private Area
0xF700
-0xF7ff
to map its range.
When you do & 0xff
you simply discard the byte in the high-order position.
moraes' answer explains why this happens, but you shouldn't be using x-user-defined
anymore as most modern (i.e. not IE) browsers, including Firefox stable, support ArrayBuffers now. Do the following instead:
// after xhr.open()
xhr.responseType = "arraybuffer";
// after xhr load
var buf =
xhr.responseBody // XHR2
|| xhr.response // FF7/Chrome 11-15
|| xhr.mozResponseArrayBuffer // FF5
;
// buf is an ArrayBuffer
Check out this gist for an example of how to get Blobs and ArrayBuffers from XHRs.
精彩评论