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

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?

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

Results

Single object

image

individual items

image

fan out

image

Demo Flow (to import)

[{"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":[]}]

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.

1 Like

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