How to display nodejs buffer buffer data as a hexadecimal string

The following code uses the SerialPort module to listen to data from a Bluetooth connection.

I expect to see the data stream in hexadecimal format printed on the console. But the console just shows some weird characters. I want to know how I can decode and display data in the console.

var serialPort = new SerialPort("/dev/tty.EV3-SerialPort", { parser: SP.parsers.raw }, false); // this is the openImmediately flag [default is true] serialPort.open(function () { console.log('open'); serialPort.on('data', function(data) { var buff = new Buffer(data, 'utf8'); //no sure about this console.log('data received: ' + buff.toString()); }); }); 
+60
buffer
Sep 18 '13 at 18:40
source share
2 answers

This code will display the data buffer as the sixth line:

 buff.toString('hex'); 
+133
Jul 01 '14 at 7:01
source share

The best answer is the easiest way to do this.

Alternative method:

 data = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); Array.prototype.map.call(new Uint8Array(data), x => ('00' + x.toString(16)).slice(-2)).join('').match(/[a-fA-F0-9]{2}/g).reverse().join(''); 
-3
Jul 25 '17 at 19:45
source share



All Articles