I am using node.js to build a tcp server, and I want to extract integers from the data received.
var net = require('net');
var server = net.createServer(function (socket) {
socket.setEncoding('ascii');
socket.addListener("data", function (data) {
var pkgDataContent = data.substr(0, 2);
});
});
server.listen(1337, "192.168.80.91");
The data received is string type, and the numbers are 1 byte, 2 bytes and 4 bytes. How to extract these 1-byte, 2-byte and 4-byte integers from a javascript string? Like the code above: pkgDataContent is a string of 2 bytes, but actually it is an integer, how to convert it to javascript number correctly?