简体   繁体   中英

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

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

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.

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