How to convert 16-bit Binary to 16- bit integer data type node- red

I want to do convert 16-bit binary to 16-bit integer Data type, How i can do ??

Welcome to the forum @Rohitkumar

It depends on what form the binary is in. Feed it into a debug node and show us what you see.

its means we will send 16-bit binary data convert to Decimal( Integer).

As I said, it depends what form the binary data is in, it could be in a buffer of individual bits, a buffer of bytes, a string of 0s and 1s, or something else.. Feed an example of it into a debug node and show us what you see, and tell us what integer value it represents.

it could be individual bit

As I said,

can you share Flow for this type of conversion on node- red??

@Rohitkumar

We cannot help if you dont provide details.

What format is your current "bits"?

  • A string of bits "1010"
  • An array of boolean [true, false, true, false]
  • An array of values [1, 0, 1, 0]
  • An object payload: { bit0: 1, bit1: 0, bit2: 1, bit3: 0}
  • A byte per value in an array?
  • a decimal value containig bits (e.g. -21,846 === 0xAAAA === 1010101010101010)

I could go on - but I hope you get the point!

I want this type of flow for Node-RED flow for is designed to convert a 32-bit binary into its 32-bit integer. we had an 32-bit (Binary) from PLC that was bit-packed with pump ON/OFF Command.

Example for
[Int32 to binary - ie, unpack bits from PLC/PAC]
(Int32 to binary - ie, unpack bits from PLC/PAC - Node-RED - OptoForums)

[{"id":"1382a7c7.0a762","type":"inject","z":"bfafb09d.34a88","name":"","topic":"","payload":"7","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":160,"wires":[["df41c583.24ff58"]]},{"id":"df41c583.24ff58","type":"function","z":"bfafb09d.34a88","name":"int32 to binary","func":"// initialize bit array to 0s\nvar bits0thru31 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];\n\nfor (i = 0; i < 32; i++){\n bits0thru31[i] = (msg.payload >> i) & 0x1;\n}\n\nvar bit0 = {payload: bits0thru31[0]};\nvar bit1 = {payload: bits0thru31[1]};\nvar bit2 = {payload: bits0thru31[2]};\nvar bit3 = {payload: bits0thru31[3]};\nvar bit4 = {payload: bits0thru31[4]};\nvar bit5 = {payload: bits0thru31[5]};\nvar bit6 = {payload: bits0thru31[6]};\nvar bit7 = {payload: bits0thru31[7]};\n\nreturn[ bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7 ];\n","outputs":"8","noerr":0,"x":580,"y":200,"wires":[["aa201119.796068"],["8756f456.0d6bf"],["6d516811.372178"],["948f3ad4.183b88"],["8f435811.a4be68"],["cdb17f80.fac8e8"],["75ff10d8.fba63"],["e5d55421.de459"]]},{"id":"aa201119.796068","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":60,"wires":},{"id":"8756f456.0d6bf","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"false","x":810,"y":100,"wires":},{"id":"6d516811.372178","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"false","x":810,"y":140,"wires":},{"id":"948f3ad4.183b88","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":180,"wires":},{"id":"b0359ef5.fc504","type":"inject","z":"bfafb09d.34a88","name":"","topic":"","payload":"13","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":200,"wires":[["df41c583.24ff58"]]},{"id":"8ee3c07b.e2f6e8","type":"inject","z":"bfafb09d.34a88","name":"","topic":"","payload":"16","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":240,"wires":[["df41c583.24ff58"]]},{"id":"634a4cb4.87fb64","type":"inject","z":"bfafb09d.34a88","name":"64","topic":"","payload":"64","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":280,"wires":[["df41c583.24ff58"]]},{"id":"8f435811.a4be68","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":220,"wires":},{"id":"d3de9e7d.6e1768","type":"inject","z":"bfafb09d.34a88","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":120,"wires":[["df41c583.24ff58"]]},{"id":"cdb17f80.fac8e8","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":260,"wires":},{"id":"75ff10d8.fba63","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":300,"wires":},{"id":"e5d55421.de459","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":340,"wires":},{"id":"38c7fe04.c374d2","type":"inject","z":"bfafb09d.34a88","name":"128","topic":"","payload":"128","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":320,"wires":[["df41c583.24ff58"]]},{"id":"f5940c68.5bf178","type":"comment","z":"bfafb09d.34a88","name":"Swap test Nodes for PLC/PAC output Node","info":"","x":400,"y":80,"wires":},{"id":"3ea62b9f.10e8a4","type":"comment","z":"bfafb09d.34a88","name":"Swap debug Nodes for groov Data Store nodes for LED gadget","info":"","x":810,"y":400,"wires":}]

Firstly, that flow cannot be imported as you did not format your post correctly. In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json


That statement is a little muddled. Please clarify. Do you mean

  • "How do I extract bits from a number"?
    Or do you mean
  • "how do I pack bits into a number?

Either way, the buffer-parser can help here.

demo flow

[{"id":"4b1a726ffdeba3cd","type":"inject","z":"52c7d8c93d68afb5","name":"a 32 bit number","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"65656565","payloadType":"num","x":1560,"y":160,"wires":[["a46039c384e28e80"]]},{"id":"a46039c384e28e80","type":"buffer-maker","z":"52c7d8c93d68afb5","name":"Conver to buffer","specification":"spec","specificationType":"ui","items":[{"name":"item1","type":"int32be","length":1,"dataType":"msg","data":"payload"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":1780,"y":160,"wires":[["2670d9ab83a7a1bd"]]},{"id":"2670d9ab83a7a1bd","type":"buffer-parser","z":"52c7d8c93d68afb5","name":"extract bits","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"pumpOn","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"valveOpen","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"breakerNormal","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"lightsOn","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"safetyNormal","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"clampAdvanced","offset":0,"length":1,"offsetbit":5,"scale":"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":1630,"y":220,"wires":[["014e2df0dfbe05b0"]]},{"id":"014e2df0dfbe05b0","type":"debug","z":"52c7d8c93d68afb5","name":"debug 24","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1840,"y":220,"wires":[]},{"id":"83e539c657c343b0","type":"comment","z":"52c7d8c93d68afb5","name":"Extract bits to object","info":"","x":1540,"y":120,"wires":[]},{"id":"50381ceef947ccc6","type":"inject","z":"52c7d8c93d68afb5","name":"a 32 bit number","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"65656565","payloadType":"num","x":1580,"y":420,"wires":[["615dca437d76d246"]]},{"id":"615dca437d76d246","type":"buffer-maker","z":"52c7d8c93d68afb5","name":"Conver to buffer","specification":"spec","specificationType":"ui","items":[{"name":"item1","type":"int32be","length":1,"dataType":"msg","data":"payload"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":1800,"y":420,"wires":[["aa972c665c77769b"]]},{"id":"aa972c665c77769b","type":"buffer-parser","z":"52c7d8c93d68afb5","name":"extract bits","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"pumpOn","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"valveOpen","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"breakerNormal","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"lightsOn","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"safetyNormal","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"clampAdvanced","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":6,"x":1610,"y":560,"wires":[["7c80cb36539f9119"],["b23065f1f18619ed"],["372db0e16df50dcf"],["21156966bce21d98"],["5a88b69e2b83892c"],["a860314977423fe2"]]},{"id":"7c80cb36539f9119","type":"debug","z":"52c7d8c93d68afb5","name":"debug 25","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1860,"y":480,"wires":[]},{"id":"0ac3886a44f0c8f7","type":"comment","z":"52c7d8c93d68afb5","name":"Extract bits to individual pins","info":"","x":1590,"y":380,"wires":[]},{"id":"b23065f1f18619ed","type":"debug","z":"52c7d8c93d68afb5","name":"debug 26","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1860,"y":520,"wires":[]},{"id":"372db0e16df50dcf","type":"debug","z":"52c7d8c93d68afb5","name":"debug 27","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1860,"y":560,"wires":[]},{"id":"21156966bce21d98","type":"debug","z":"52c7d8c93d68afb5","name":"debug 28","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1860,"y":600,"wires":[]},{"id":"5a88b69e2b83892c","type":"debug","z":"52c7d8c93d68afb5","name":"debug 29","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1860,"y":640,"wires":[]},{"id":"a860314977423fe2","type":"debug","z":"52c7d8c93d68afb5","name":"debug 30","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1860,"y":680,"wires":[]},{"id":"c75aecbedfe5d0d6","type":"comment","z":"52c7d8c93d68afb5","name":"Extract all bits as array","info":"","x":1590,"y":860,"wires":[]},{"id":"dcb67d336153e2a8","type":"inject","z":"52c7d8c93d68afb5","name":"a 32 bit number","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"65656565","payloadType":"num","x":1600,"y":900,"wires":[["6082b0e72760d87e"]]},{"id":"6082b0e72760d87e","type":"buffer-maker","z":"52c7d8c93d68afb5","name":"Conver to buffer","specification":"spec","specificationType":"ui","items":[{"name":"item1","type":"int32be","length":1,"dataType":"msg","data":"payload"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":1820,"y":900,"wires":[["fb2067f470cb49bc"]]},{"id":"fb2067f470cb49bc","type":"buffer-parser","z":"52c7d8c93d68afb5","name":"extract 32 bits","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"bits","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1650,"y":980,"wires":[["66048cafcd521719"]]},{"id":"66048cafcd521719","type":"debug","z":"52c7d8c93d68afb5","name":"debug 31","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1850,"y":980,"wires":[]}]

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