# How to Extracting a binary integer data from a msg.payload?

**URL:** <https://discourse.nodered.org/t/how-to-extracting-a-binary-integer-data-from-a-msg-payload/44844>\
**Category:** General\
**Created:** [28 April 2021 08:24 UTC](https://discourse.nodered.org/t/how-to-extracting-a-binary-integer-data-from-a-msg-payload/44844 "2021-04-28T08:24:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fef\_38](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fef_38/32/40902_2.png) [@Fef\_38](https://discourse.nodered.org/u/Fef_38)\
**Post date:** [28 April 2021 08:24 UTC](https://discourse.nodered.org/t/how-to-extracting-a-binary-integer-data-from-a-msg-payload/44844/1 "2021-04-28T08:24:55Z")

</div>

Hi,  
I’m a beginner and I don’t understand what happene with “binary” node  
I receive from MQTT node a msg.payload in binary format: the fields are 12 32bit integers in little endian, like bellow:  
F0CC0100581B0000D02F0000DE670000525300003A570000225B00000A5F0000F2620000DA660000C26A0000AA6E000041  
I have not separator between each integer.  
To read my data I use the binary node and I extract the data like it:  
l32 =\> d0, l32 =\> d1, l32 =\> d2, …. l32 =\> d11  
And I read that :  
d0 :0x1fdfd (d130557)  
d1: 0x1b58  
Etc…  
d0 (0x1fdfd ) is wrong, the data is F0CC0100 in little endian and 0x0001CCF0 = 118000

I saw when a byte has bit 8 = 1 the conversion is false…  
I try with -l31 but the result is bad too!  
Do you know why? Any suggestion?

---

<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:** [28 April 2021 09:06 UTC](https://discourse.nodered.org/t/how-to-extracting-a-binary-integer-data-from-a-msg-payload/44844/2 "2021-04-28T09:06:30Z")

</div>

If you dont mind installing a contrib node, then node-red-contrib-buffer-parser can do all the heavy lifting for you.

Here I demonstrate 3 ways you can convert your numbers (1 object, multiple payloads or single items (fan out) ...

### Flow

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

### Results

#### Single object

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

#### individual items

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/74a4b85970dcf8fe97d290907c0d05b621db2c70.png)

#### fan out

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8bb79fa0aa64a2ec16d5d53f46134695eba8ca8d.png)

#### Demo Flow (to import)

```auto
[{"id":"f3710ef8.6d74e","type":"inject","z":"59c1e9ca.dd6988","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"F0CC0100581B0000D02F0000DE670000525300003A570000225B00000A5F0000F2620000DA660000C26A0000AA6E000041","payloadType":"str","x":550,"y":480,"wires":[["75360cfb.5b8a34"]]},{"id":"75360cfb.5b8a34","type":"function","z":"59c1e9ca.dd6988","name":"faking the data","func":"msg.payload = Buffer.from(\"F0CC0100581B0000D02F0000DE670000525300003A570000225B00000A5F0000F2620000DA660000C26A0000AA6E000041\", \"hex\")\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":480,"wires":[["6edebc31.7e1374","4bec1b48.a40724","787b9f2c.42702"]]},{"id":"6edebc31.7e1374","type":"buffer-parser","z":"59c1e9ca.dd6988","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int32le","name":"d0","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d1","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d2","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d3","offset":12,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d4","offset":16,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d5","offset":20,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d6","offset":24,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d7","offset":28,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d8","offset":32,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d9","offset":36,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d10","offset":40,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d11","offset":44,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":910,"y":440,"wires":[["5c4011b8.a86e3"]]},{"id":"5c4011b8.a86e3","type":"debug","z":"59c1e9ca.dd6988","name":"single object (ideal for sending to DB or performing calcs)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":440,"wires":[]},{"id":"3258f385.476e3c","type":"comment","z":"59c1e9ca.dd6988","name":"1 object","info":"","x":890,"y":400,"wires":[]},{"id":"4bec1b48.a40724","type":"buffer-parser","z":"59c1e9ca.dd6988","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int32le","name":"d0","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d1","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d2","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d3","offset":12,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d4","offset":16,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d5","offset":20,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d6","offset":24,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d7","offset":28,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d8","offset":32,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d9","offset":36,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d10","offset":40,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d11","offset":44,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":910,"y":520,"wires":[["b9b22b0a.499ef8"]]},{"id":"787b9f2c.42702","type":"buffer-parser","z":"59c1e9ca.dd6988","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int32le","name":"d0","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d1","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d2","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d3","offset":12,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d4","offset":16,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d5","offset":20,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d6","offset":24,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d7","offset":28,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d8","offset":32,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d9","offset":36,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d10","offset":40,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"d11","offset":44,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":12,"x":910,"y":680,"wires":[["33046ab8.394cd6"],["3565d3dd.ba804c"],["565aa379.7c3a5c"],[],[],[],[],[],[],[],[],[]]},{"id":"af5317b2.9e2c98","type":"comment","z":"59c1e9ca.dd6988","name":"individual items","info":"","x":920,"y":480,"wires":[]},{"id":"c34045b4.4ba578","type":"comment","z":"59c1e9ca.dd6988","name":"fan out","info":"","x":890,"y":560,"wires":[]},{"id":"33046ab8.394cd6","type":"debug","z":"59c1e9ca.dd6988","name":"d0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":580,"wires":[]},{"id":"3565d3dd.ba804c","type":"debug","z":"59c1e9ca.dd6988","name":"d1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":611,"wires":[]},{"id":"b9b22b0a.499ef8","type":"debug","z":"59c1e9ca.dd6988","name":"individual items (ideal for MQTT)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":520,"wires":[]},{"id":"565aa379.7c3a5c","type":"debug","z":"59c1e9ca.dd6988","name":"d2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":640,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Fef\_38](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fef_38/32/40902_2.png) [@Fef\_38](https://discourse.nodered.org/u/Fef_38)\
**Post date:** [28 April 2021 10:45 UTC](https://discourse.nodered.org/t/how-to-extracting-a-binary-integer-data-from-a-msg-payload/44844/3 "2021-04-28T10:45:50Z")

</div>

Hi,  
Thank you very much for your reply  
From your demo flow I found my mistake!  
In the begging I used the payload with raw values  
Now I transform the payload into a Hex buffer:  
mvk=msg.payload.data;  
msg.payload = Buffer.from(mvk, "hex")  
return msg;  
after I use the binary node without problem...  
And now All work well!  
Thank again.

---

<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:** [27 June 2021 10:46 UTC](https://discourse.nodered.org/t/how-to-extracting-a-binary-integer-data-from-a-msg-payload/44844/4 "2021-06-27T10:46:13Z")

</div>

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