I am using node.js to build a tcp server and I want to extract integers from the received data.
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 received data is a 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 it is actually an integer, how to convert it to javascript correctly?
source
share