Java array byte for array in Javascript

I'm trying to send some raw data from the Java Servlet to JavaScript via binaryType via Websocket. I read all about the byte buffer, typed arrays and JavaScript data representation, but still I think that I am missing an image

My question is how to convert an array of 8 bytes to a JavaScript number?

I have the following byte array that should represent this double value -1.4960627518157586E23

0 = -60 1 = -65 2 = -82 3 = 44 4 = 36 5 = 69 6 = -96 7 = 64 

Can someone help me in serializing byte data from Java to JavaScript types, please?

Great importance.

+5
source share
1 answer

Javascript Reading

Reading binary data in Javascript requires some effort, but someone conveniently made a piece of code that helps with this: http://jsfromhell.com/classes/binary-parser

We can use it like this:

 var parser = BinaryParser(true); var outputVal = parser.toDouble([-60, -65, -82, 44, 36, 69, -96, 64]); 

Reading in Java

If you get data in java, you can use java.io.DataInputStream to read primitive values ​​from a stream, for example:

 byte[] dataArray = new byte[] {-60, -65, -82, 44, 36, 69, -96, 64}; DataInputStream stream = new DataInputStream(new ByteArrayInputStream(dataArray)); double val = stream.readDouble(); out.println(val); // -1.4960627518157586E23 
0
source

All Articles