Wednesday, 26 April 2017

Reading signed int16 from a socket

We are trying to read singed int16 values from a a socket.

For that we create a socket:

var client = new net.Socket();
client.setEncoding(null); // prevent string-parsing
client.connect(8990, '10.30.0.1',  () => { /* send data */ });

We used wireshark to capture the data send to us:

Data (12 bytes)

Data: 0000000000030000fdff0000

[Length: 12]

We read data using the 'data' event

client.on('data', (data) => {
    var buffer = Buffer.from(data);
});

Now data has a length of 12 but when we create the buffer it has a length of 16 and has different values

Data of the buffer:

[0,0,0,0,0,3,0,0,239,191,189,239,191,189,0,0]

We tried various combinations to read the int16 values from the stream but all failed...

We also tried using https://github.com/keichi/binary-parser (which we would love to use), but the resulting value was just a '0'.

Can anyone point us how we can fix this?



via sra

No comments:

Post a Comment