Get bits from an integer

Hi
I have an string of number Ex = 01001010 and I want to split it to 0,1,0,0,1,0 I am able to get that but it ignoring the zero in the at zero index
I have attached flow below

[{"id":"60b50f5c.3a867","type":"debug","z":"7e597862c5e169b5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":430,"y":1180,"wires":[]},{"id":"fcc3e30d00183024","type":"inject","z":"7e597862c5e169b5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0101001","payloadType":"num","x":140,"y":1180,"wires":[["743b24f6.14e19c"]]},{"id":"743b24f6.14e19c","type":"function","z":"7e597862c5e169b5","name":"convert","func":"let digits = msg.payload.toString().split('');\nlet realDigits = digits.map(Number)\n\nfunction sendOut(digit){\n    node.send({payload:digit})\n}\n\nrealDigits.forEach(sendOut)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":1180,"wires":[["60b50f5c.3a867"]]}]

Thank You

You are not injecting "0101001" but the number 0101001 (which is 101001)
Untitled 1

2 Likes

Are you sure that is what you want to do? If so then why?

Hello,
I have several sensors, and each one of them is giving me binary values of 1 bit. These data are summed, and then they are delivered to Node-red as integer values. I want to display the state of these sensors in the NR Dashboard, thus I must first convert the integers to binary and then split the binary string in order to display the status of each sensor separately.

If you have it as an integer number then you don't need to convert to a string then extract the bits, you can extract the bits directly. However, before I help you to code that, please feed the data that node-red is receiving into a debug node and show us what you get, and tell us what bit values you want to get out.

I am feeding int value to bit unloader to convert to binary and then splitting it to 16 bit length in which I am using first 11 bit values to show the status of 11 induvial sensor

I have attached my flow below

[{"id":"743b24f6.14e19c","type":"function","z":"7e597862c5e169b5","name":"B Split ","func":"let digits = msg.payload.toString().split('');\nlet realDigits = digits.map(Number)\n\nfunction sendOut(digit){\n    node.send({payload:digit})\n}\n\nrealDigits.forEach(sendOut)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":1060,"wires":[["3f3350fd4d38b875"]]},{"id":"fc1b8caae028857f","type":"inject","z":"7e597862c5e169b5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"9","payloadType":"num","x":350,"y":1060,"wires":[["2142ba57fa993ebd"]]},{"id":"3f3350fd4d38b875","type":"debug","z":"7e597862c5e169b5","name":"debug 9","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":1060,"wires":[]},{"id":"7818b2e05b4e2c89","type":"debug","z":"7e597862c5e169b5","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":1100,"wires":[]},{"id":"2142ba57fa993ebd","type":"bitunloader","z":"7e597862c5e169b5","name":"","mode":"string","prop":"payload","padding":"16","x":490,"y":1060,"wires":[["7818b2e05b4e2c89","743b24f6.14e19c"]]}]

With the buffer-parser node, you can get all values in 1 object (then you can simply use them like msg.payload.status.bit0) without looping / splitting etc.

image

[{"id":"fc1b8caae028857f","type":"inject","z":"b851e68919625d02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[9]","payloadType":"json","x":2210,"y":600,"wires":[["61d003e0d43d3def"]]},{"id":"7818b2e05b4e2c89","type":"debug","z":"b851e68919625d02","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2360,"y":660,"wires":[]},{"id":"61d003e0d43d3def","type":"buffer-parser","z":"b851e68919625d02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"16bitbe","name":"status","offset":0,"length":1,"offsetbit":0,"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":2360,"y":600,"wires":[["7818b2e05b4e2c89"]]}]
1 Like

If you want individual values, you can set them up and map them to named items from any bit you specify...

[{"id":"fc1b8caae028857f","type":"inject","z":"b851e68919625d02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[9]","payloadType":"json","x":2210,"y":600,"wires":[["61d003e0d43d3def"]]},{"id":"7818b2e05b4e2c89","type":"debug","z":"b851e68919625d02","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2360,"y":660,"wires":[]},{"id":"61d003e0d43d3def","type":"buffer-parser","z":"b851e68919625d02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"power","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"fault","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"running","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"stopped","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"overrun","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"underrun","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""},{"type":"bool","name":"torqueOver","offset":0,"length":1,"offsetbit":6,"scale":"1","mask":""},{"type":"bool","name":"softLimit","offset":0,"length":1,"offsetbit":7,"scale":"1","mask":""},{"type":"bool","name":"hardLimit","offset":0,"length":1,"offsetbit":8,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2360,"y":600,"wires":[["7818b2e05b4e2c89"]]}]
1 Like

(Last one)

If you want well defined names (so you can use msg.payload.running etc) it can do that too..

image

[{"id":"fc1b8caae028857f","type":"inject","z":"b851e68919625d02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[9]","payloadType":"json","x":2210,"y":600,"wires":[["61d003e0d43d3def"]]},{"id":"7818b2e05b4e2c89","type":"debug","z":"b851e68919625d02","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2360,"y":660,"wires":[]},{"id":"61d003e0d43d3def","type":"buffer-parser","z":"b851e68919625d02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"power","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"fault","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"running","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"stopped","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"overrun","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"underrun","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""},{"type":"bool","name":"torqueOver","offset":0,"length":1,"offsetbit":6,"scale":"1","mask":""},{"type":"bool","name":"softLimit","offset":0,"length":1,"offsetbit":7,"scale":"1","mask":""},{"type":"bool","name":"hardLimit","offset":0,"length":1,"offsetbit":8,"scale":"1","mask":""}],"swap1":"swap16","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":2360,"y":600,"wires":[["7818b2e05b4e2c89"]]}]
1 Like

Woooo - Steve, you have opened a door to a place I've never seen.
I can see a number of situations where my IoT students could use the buffer-parser.

2 Likes

Hi
Couple of Questions

  1. Can I get the value as ON and OFF Instead of True or False.
  2. what is the best method to reverse this process Getting integer from Binary Values

Thanks

Well since node-red runs on NodeJS (AKA JavaScript) - you can do whatever you like...

[{"id":"fc1b8caae028857f","type":"inject","z":"b851e68919625d02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[9]","payloadType":"json","x":2710,"y":300,"wires":[["61d003e0d43d3def"]]},{"id":"7818b2e05b4e2c89","type":"debug","z":"b851e68919625d02","name":"before","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3030,"y":300,"wires":[]},{"id":"61d003e0d43d3def","type":"buffer-parser","z":"b851e68919625d02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"power","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"fault","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"running","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"stopped","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"overrun","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"underrun","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""},{"type":"bool","name":"torqueOver","offset":0,"length":1,"offsetbit":6,"scale":"1","mask":""},{"type":"bool","name":"softLimit","offset":0,"length":1,"offsetbit":7,"scale":"1","mask":""},{"type":"bool","name":"hardLimit","offset":0,"length":1,"offsetbit":8,"scale":"1","mask":""}],"swap1":"swap16","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":2860,"y":300,"wires":[["7818b2e05b4e2c89","ff89ed8f2744be8f"]]},{"id":"ff89ed8f2744be8f","type":"function","z":"b851e68919625d02","name":"true/false => ON/OFF","func":"for (let prop in msg.payload) {\n    if (msg.payload.hasOwnProperty(prop)) {\n        msg.payload[prop] = msg.payload[prop] === true ? \"ON\" : \"OFF\"\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2820,"y":360,"wires":[["4bf5226f875fee1b"]]},{"id":"4bf5226f875fee1b","type":"debug","z":"b851e68919625d02","name":"after","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3030,"y":360,"wires":[]}]

What is a "Binary Value" - Binary is simply a base2 representation of an integer.
e.g. 0b1111 === 15 === 0xF === 017 - proof...
image

So what exactly do you mean? A string of "1010101010101"? Where is your value coming from and what does it look like in the debug window?

A post was split to a new topic: Combine and convert into an integer value

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