I read a binary file (of 16bit integers) in my node.js code and send it to client using connection.sendUTF(data) of websocket. At the javascript code of the client side I receive an array of UTF8 and would like to convert back every pair to an int16 ( numbers are unsigned, if makes it simple).
I came up with this function to be used at the client side and was wondering if it is correct.
function char2BigEndian16(hiChar, lowChar){
return ((lowChar.charCodeAt(0)& 0x00FF) << 8) | (hiChar.charCodeAt(0) & 0xFF);
}
and here is how I call it to convert first to elements of array data to a 2-byte integer
var i16 = char2BigEndian16(data[1], data[0]);
If this help here is my node.js code snipped:
var fs = require('fs');
filename = "test.txt";
fs.readFile(filename, 'utf8', function(err, data) {
if (err) {
console.log('Error!!');
return console.log(err);
}
connection.sendUTF(data);
});
and here is how I deal with the received data on the client side
connection.onmessage = function(e) {
console.log(e.data.length);
}