如何将Node.js原始缓冲区数据显示为十六进制字符串 [英] How to display nodejs raw Buffer data as Hex string

查看:431
本文介绍了如何将Node.js原始缓冲区数据显示为十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码使用SerialPort模块来侦听来自蓝牙连接的数据.

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());
 });  
});

推荐答案

以下代码将数据缓冲区显示为十六进制字符串:

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

buff.toString('hex');

这篇关于如何将Node.js原始缓冲区数据显示为十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆