Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Buffer to integer in JavaScript

I get this buffer from my serial port:

<Buffer 04 02 08 dc>

The 2nd byte says how many bytes do I need to parse my from response data. So I need to parse these two bytes 08 dc, and convert them to long unsigned.

How do I do it in JavaScript on a Node.js server?

like image 831
bmvr Avatar asked Jun 19 '26 06:06

bmvr


1 Answers

buf.readInt32BE([offset]) and buf.readInt32LE([offset]) let you read a 32b int from 4-bytes starting at offset.

Reads a signed 32-bit integer from buf at the specified offset with the specified endian format (readInt32BE() returns big endian, readInt32LE() returns little endian).

Integers read from a Buffer are interpreted as two's complement signed values.

like image 100
Mike Samuel Avatar answered Jun 21 '26 20:06

Mike Samuel



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!