# How to Decode 16 Bit signed Integer

**URL:** <https://discourse.nodered.org/t/how-to-decode-16-bit-signed-integer/8112>\
**Category:** General\
**Created:** [18 February 2019 17:06 UTC](https://discourse.nodered.org/t/how-to-decode-16-bit-signed-integer/8112 "2019-02-18T17:06:38Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 February 2019 15:00 UTC](https://discourse.nodered.org/t/how-to-decode-16-bit-signed-integer/8112/10 "2019-02-23T15:00:38Z")

</div>

> [@aargollo](#):
>
> I can´t find reference how to use readInt16LE in the 2 byte.  
> Do you can advise me how to fix it?

not really sure where your going with this???

So, in an effort to put you on the right track I will try to explain what I think you are attempting to do.

If i'm not mistaken, the data you see in the payload is a [NodeJS Buffer](https://nodejs.org/api/buffer.html).

Looking at the decoded image you attached in your first message, the data is formatted like a protocol (I don't know what protocol) however, the picture gives some clues to the end part of the message.

Based on what I can see, you should really decode the whole msp.payload reply (since the parts of data before the "measured value" are probably important)

That said, the below should get you started....

**testcode...**

```javascript
//fake the msg.payload (simulate serial reply)
var msg = {};
msg.payload = new Buffer.from([0x40, 0x1, 0x2, 0x4, 0x4, 0x1, 0xa, 0xa6, 0x18, 0x7e, 0x1, 0x0, 0x0, 0x0, 0x29, 0x17, 0x0c]);

//decode the status record
var statusRecord = {};
statusRecord.attribute = msg.payload.readInt16LE(11);
statusRecord.success = msg.payload.readInt8(13);
statusRecord.dataType = msg.payload.readInt8(14);
statusRecord.measuredValue = msg.payload.readInt16LE(15);

//todo: Check the success flag is 0
//todo: Check dataType before calling the next buffer.readXXXX
// e.g if dataType is byte then call readInt8() to get measuredValue

if(statusRecord.success === 0){
    console.log(`Read was successful :)`);
    console.log(statusRecord);
    console.log(`Measured Value: ${statusRecord.measuredValue / 100.0} [*C]`);
} else {
    console.log(`Read was unsuccessful! :(`);
}

```

**output...**

```console
Read was successful :)
{ attribute: 0, success: 0, dataType: 41, measuredValue: 3095 }
Measured Value: 30.95 [*C]

```

You can play with it here \>\> [Compile and Execute C++0x Onlinej](http://tpcg.io/McQH1S)

Hope that helps

---

_[View the full topic](https://discourse.nodered.org/t/how-to-decode-16-bit-signed-integer/8112)._
