Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js convert hexadecimal number to byteArray

Tags:

node.js

buffer

I want to send a raw buffer using bluetooth connection. The content is a hex number. Currently I split the number manually to an byte array. Is there any function that can help me convert the number to byte array?

//var data = 0x250001000192CD0000002F6D6E742F72;
var data = new Buffer([0x25,0x00,0x01,0x00,0x01,0x92,0xCD,0x00,0x00,0x00,0x2F,0x6D,0x6E,0x74,0x2F,0x72]);
serialPort.write(data);
like image 982
GingerJim Avatar asked Sep 18 '13 19:09

GingerJim


2 Answers

In new versions of node (6+) the new Buffer() interface is deprecated. Use:

Buffer.from("250001000192CD0000002F6D6E742F72", "hex")

instead.

Find more info on this using this link below https://nodejs.org/api/buffer.html#buffer_static_method_buffer_from_string_encoding

like image 187
Brad Avatar answered Oct 22 '22 05:10

Brad


new Buffer("250001000192CD0000002F6D6E742F72", "hex")
like image 74
SLaks Avatar answered Oct 22 '22 04:10

SLaks