I am sending hex code to machine in nodejs socket application and receive hex answer from code below:
socket.on('data', function(data){
console.log('Socket:'+Buffer.from(data).toString('hex'));
});
In console I receive this: 0104000100150104125d2befbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbd00efbfbdefbfbd
What is expected:
01 04 00 01 00 15 01 04 12 5D 2B FF FF FF FF FF FF FF FF FF FF FF FF 80 00 FF FF
I don't understand why there is additional data.
Seems data is string, not buffer.
just try this code
const data = Buffer.from('01 04 00 01 00 15 01 04 12 5D 2B FF FF FF FF FF FF FF FF FF FF FF FF 80 00 FF FF'.split(' ').map(x => parseInt(x, 16)));
console.log(Buffer.from(data).toString('hex'));
const str = data.toString(); // <- problem!!!
console.log(Buffer.from(str).toString('hex'));
You can't send binary buffer directly, just convert to hex string before and to buffer after for example.
Read this topic Node Buffers, from utf8 to binary
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.