# 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)

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 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.