# Conversion of hexadecimal (16 bits signed) values to decimal

**URL:** <https://discourse.nodered.org/t/conversion-of-hexadecimal-16-bits-signed-values-to-decimal/67940>\
**Category:** General\
**Created:** [19 September 2022 20:00 UTC](https://discourse.nodered.org/t/conversion-of-hexadecimal-16-bits-signed-values-to-decimal/67940 "2022-09-19T20:00:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [19 September 2022 20:00 UTC](https://discourse.nodered.org/t/conversion-of-hexadecimal-16-bits-signed-values-to-decimal/67940/1 "2022-09-19T20:00:49Z")

</div>

Good evening, once again I am facing a conversion problem. I receive this data in this format. They are the values of voltage, current (which can be negative) and temperature.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d515828e3732c4145569c2047f5ae1014a555058.png)

Debug result: `{"timestamp":1663615971682,"ext":false,"canid":854,"dlc":6,"rtr":false,"data":[107,19,127,255,228,0]}`

It gives about these values. (Order of idea)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/45b78a55f4d0eb1ac33cb9a6a8aa8c946a24d013.png)  
49.7 Volt, -1.5 Amperes, 24°

Obviously I want to convert them into a readable format. I have tried to copy and paste examples found on the site without much success. I can do it for a positive value like the voltage, but as soon as it becomes negative (Current discharge), it doesn't work anymore.  
To decode the frame, I use this documentation. It mentions coding in **Little Endian**.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/304a21cd2e35e3a97b3f6132719603ec9752b119.png)

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![Sineos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sineos/32/3440_2.png) [@Sineos](https://discourse.nodered.org/u/Sineos)\
**Post date:** [19 September 2022 20:13 UTC](https://discourse.nodered.org/t/conversion-of-hexadecimal-16-bits-signed-values-to-decimal/67940/2 "2022-09-19T20:13:14Z")

</div>

I'm using [node-red-contrib-buffer-parser (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-buffer-parser) to handle buffers of my MODBUS devices.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [19 September 2022 20:19 UTC](https://discourse.nodered.org/t/conversion-of-hexadecimal-16-bits-signed-values-to-decimal/67940/3 "2022-09-19T20:19:02Z")

</div>

Thanks, I also use it, see the top of the flow, for another pattern with success (SOC & SOH).  
But for this one I can't do it.  
It should certainly do the job, but I don't know how to describe the fields requested in the node for this particular case.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [19 September 2022 20:21 UTC](https://discourse.nodered.org/t/conversion-of-hexadecimal-16-bits-signed-values-to-decimal/67940/4 "2022-09-19T20:21:48Z")

</div>

Here is an example of how to use the buffer-parser ..  
not sure but you may need to pass the `msg.payload.data` array from a Function node to convert it to an actual Buffer ?

```auto
[{"id":"f49b67038f22e562","type":"inject","z":"54efb553244c241f","name":"Buffer","props":[{"p":"payload.data","v":"[107,19,127,255,228,0]","vt":"bin"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":450,"y":1880,"wires":[["e0f41245d2ef1999"]]},{"id":"e0f41245d2ef1999","type":"buffer-parser","z":"54efb553244c241f","name":"","data":"payload.data","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16le","name":"volts","offset":0,"length":1,"offsetbit":0,"scale":"0.01","mask":""},{"type":"int16le","name":"current","offset":2,"length":1,"offsetbit":0,"scale":"0.01","mask":""},{"type":"int16le","name":"temperature","offset":4,"length":1,"offsetbit":0,"scale":"0.1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":650,"y":1920,"wires":[["06e59ccf97c82817"]]},{"id":"06e59ccf97c82817","type":"debug","z":"54efb553244c241f","name":"debug 28","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":1920,"wires":[]},{"id":"a0875f6b15288714","type":"inject","z":"54efb553244c241f","name":"Array ?","props":[{"p":"payload.data","v":"[107,19,127,255,228,0]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":290,"y":1960,"wires":[["5c2aaff5157402ac"]]},{"id":"5c2aaff5157402ac","type":"function","z":"54efb553244c241f","name":"to Buffer","func":"msg.payload.data = Buffer.from(msg.payload.data)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":1960,"wires":[["e0f41245d2ef1999"]]}]

```

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [19 September 2022 20:36 UTC](https://discourse.nodered.org/t/conversion-of-hexadecimal-16-bits-signed-values-to-decimal/67940/5 "2022-09-19T20:36:32Z")

</div>

Andy, I would just like to say " **whooowww**" it works with the help of the function node.  
Many thanks for the more than quick help 🚀

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fcfe7dad91debe6da9ad9553dc8338192ab68d3f.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 October 2022 20:36 UTC](https://discourse.nodered.org/t/conversion-of-hexadecimal-16-bits-signed-values-to-decimal/67940/6 "2022-10-03T20:36:57Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
