Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display nodejs raw Buffer data as Hex string

Tags:

node.js

buffer

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

I am expecting to see a stream of data in Hexadecimal format printed in console. But the console just shows some weird simbols. I want to know how can I decode and display the data in 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());  });   }); 
like image 310
GingerJim Avatar asked Sep 18 '13 18:09

GingerJim


People also ask

How do you convert a buffer to a string value in node JS?

Buffers have a toString() method that you can use to convert the buffer to a string. By default, toString() converts the buffer to a string using UTF8 encoding. For example, if you create a buffer from a string using Buffer. from() , the toString() function gives you the original string back.

What is buffer data type in node JS?

What Are Buffers? The Buffer class in Node. js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data.


1 Answers

This code will show the data buffer as a hex string:

buff.toString('hex'); 
like image 79
Seryh Avatar answered Sep 25 '22 05:09

Seryh