简体   繁体   English

如何在 Node.js 中将输出十六进制转换为十进制?

[英]How to convert output hex to decimal in Node.js?

I'm trying to get data from a bluetooth device(heart-rate monitor) where it will show the values of heart rate, pulse rate.我正在尝试从蓝牙设备(心率监视器)获取数据,它将显示心率、脉搏率的值。 when i run the program in terminal the output in comes in hex so any ideas how to display the output in decimal.当我在终端中运行程序时,输出以十六进制形式出现,因此任何想法如何以十进制显示输出。 Thanks..谢谢..

const bluetooth = require('node-bluetooth');

// create bluetooth device instance

const device = new bluetooth.DeviceINQ();

var num = 0;

device
.on('finished',  console.log.bind(console, 'finished'))
.on('found', function found(address, name){
console.log('Found: ' + address + ' with name ' + name);

device.findSerialPortChannel(address, function(channel){
console.log('Found RFCOMM channel for serial port on %s: ', name,     'channel:',channel);

// // make bluetooth connect to remote device
bluetooth.connect(address, channel, function(err, connection){

console.log('North Vision Patient Monitor')

if(err) return console.error(err);

connection.write(new Buffer([0x55,0xAA,0x00], 'utf-8'), () => {

console.log('Send Data')


connection.write(new Buffer([
  0x0b,
  0x40,
  0x85,
  0xB6,
  0xFB,
  0x10,
  0x3C,
  0xC8,
  0xFF,
  0xB4,
  0x28,
  0x28,
  0x0A,
  0x64,
  0x5A,
  0xB4,
  0x3C,
  0x78,
  0x32,
  0xA0,
  0x32,
  0x6E,
  0x46,
  0xB4,
  0x28,
  0x04,
  0x07,
  0x46,
  0x0A,
  0x0A,
  0x00,
  0x00], 'utf-8'), () => {

    console.log('Receive Data')


    });

});


 connection.on('data', (buffer) => {
 console.log('received message:', buffer,'====',num);
 num++;
 });


});

console.log('finished')

});

 // // make bluetooth connect to remote device
// bluetooth.connect(address, channel, function(err, connection){
// if(err) return console.error(err);

// connection.on('data', (buffer) => {
// console.log('received message:', buffer.toString());
// });

// connection.write(new Buffer('Hello!', 'utf-8'));
// });

}).inquire();

Output:输出:

North Vision Patient Monitor Send Data Receive Data Buffer 1b a1 00 00 00 00 00 00 00 00 00 00 57 55 51 4c 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 North Vision 患者监护仪发送数据 接收数据缓冲区 1b a1 00 00 00 00 00 00 00 00 00 00 57 55 51 4c 48 00 00 00 00 00 00 00 00 00 00 00 0 0 0

cons bufferInHex = Buffer.from('some string') // init a buffer
console.log(new Uint8Array(bufferInHex)) // display buffer in decimal format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM