Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read unsigned int in Javascript?

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?

like image 370
Fluffy Avatar asked Sep 13 '12 13:09

Fluffy


People also ask

How do I read an unsigned integer in Java?

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.

How to convert signed 32-bit integers to unsigned integers in JavaScript?

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 />');

What is the type of unsigned int in Java?

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)

How to read an unsigned 8 bit integer from a buffer?

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.


Video Answer


1 Answers

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.

like image 193
raina77ow Avatar answered Oct 05 '22 20:10

raina77ow