Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Hex string into binary data into a buffer

Tags:

node.js

Sometimes from network transmissions/usdb devices you receive the data has a hexadimal string eg:

"12ADFF1345"

These type of string I want somehow to be converted into a binary equivalent into a buffer, in order to perform a some mathematical or binary operations on them.

Do you know how I can achieve that?

like image 734
Dimitrios Desyllas Avatar asked Sep 09 '26 00:09

Dimitrios Desyllas


1 Answers

Use the builtin Buffer class :

let buf1 = Buffer.from('12ADFF1345', 'hex');

let value = buf1.readInt32LE(0);
let value2 = buf1.readInt16LE(2);
console.log(value,value2);


>> 335523090 5119
// '13ffad12' '13FF' (LE) 
>> 313392915 -237
// '12ADFF13' 'ff13' (BE)

https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding

like image 170
corn3lius Avatar answered Sep 11 '26 13:09

corn3lius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!