I'm writing a binary string from the server like this:
header('Content-type: application/octet-stream');
echo $data = pack('C*', 0, 10, 100, 127, 128, 200, 250, 255, 256);
and reading it with js and jDataView lib this way:
$.get('/get', function(text) {
var view = new jDataView(text);
for (var i = 0; i < 20; i++) {
console.log(i, view.getUint8(i));
}
});
The problem is that I can only read the values that are less than 128. jDataView uses getCharCode
at to read Uint
and it returns 65533 for each of the bigger values.
How to I get the values in js?
Use an UnsignedIntegerfrom Guava Use an intand interpret the bits as unsigned (described below) An unsigned int An intis always signed in Java, but nothing prevents you from viewing an intsimply as 32 bits and interpret those bits as a value between 0 and 264.
The only JavaScript operator that works using unsigned 32-bit integers is >>>. You can exploit this to convert a signed-integer-in-Number you've been working on with the other bitwise operators to an unsigned-integer-in-Number: document.write (((1<<31)>>>0)+'<br />');
Unsigned int in Java. Java does not have unsigned data types. Your options are: Use a long. Use an UnsignedInteger from Guava. Use an int and interpret the bits as unsigned (described below)
The Buffer.readUInt8 () method is used to read an unsigned 8 bit integer from a Buffer object. Parameters: This method accepts a single parameter offset which specifies the position of buffer object. It represents the number of bytes to skip before starting to read.
It works just fine when you change the headers sent (by PHP file) with...
header('Content-Type: text/plain; charset=x-user-defined')
Without this header (and tweaking of how XHR response should be processed; this article describes the process in details) it's the text
value that becomes messed up: all 'invalid' (> 127) characters will be literally replaced by '\uFFFD'
ones.
Of course, it's not possible to extract the original values from these characters, so it's not a bug of jDataView, in my opinion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With