I just extracted a value from a string, and thanks to this forum for this.
Now I would like to convert the value in substring n.5 (the value is in hex, so n.4 bit) in binary and obtain n.4 outputs like the bit.
For example:
hex: C I would like to obtain:
output 0
output 0
output 1
output 1
parseInt(msg.payload.substr, 2)
return msg;
I tried the code up but I am not able to urderstand some points, in particular how to obtain 4 outputs, one for each bit. Can you please help me? Do you have some suggestion or some similar project that can help me?
Rather than trying to do lots of things on one line, try to do it step-by-step.
For one thing it is more likely to pass the 6 month test, that after 6 months you can still understand it.
Your second line parseInt(msg.payload.substr, 2)
your don't say what you are declare this to be
I would expect something like msg.payload = <your line here>
One other thing. Nowhere do you declare that the number you are passing is hex, so how do you expect Node-RED to know?
The page in the docs might be useful. https://nodered.org/docs/user-guide/writing-functions
for example the section about logging events to allow you to send messages from in the middle of the function to the debug panel.
I understand that I receive a string > I've to convert in hex > I've to convert in bin > I've to split the 4 bit in 4 outputs.
I only found how to do the central part, at the moment I am trying to understand how to compile the function to convert string in hex...
Really ? - OP is trying to get 4 outputs out - 1 for each bit - so a number is just a number whether hex or not...
A combination of bitwise operators is what will be required https://www.w3schools.com/js/js_bitwise.asp
eg
var m0 = msg.payload & 0x01;
var m1 = msg.payload & 0x02 >>> 1;
var m2 = msg.payload & 0x04 >>> 2;
var m3 = msg.payload & 0x08 >>> 3;
return [{payload:m0},{payload:m1},{payload:m2},{payload:m3}];
What have you searched for so far, and what results did you get? Try google first, it might give answers quicker than waiting on people here for every step: javascript convert string to number.
Or try the forum search, I’ve posted the basic solution including on dealing with binary numbers a few times already.
Do you know what the 10 stands for in your parseInt call? It's the base system to use, so 10 means base 10 aka decimal. I'm not sure why you put parentheses around it though.
This will indeed not work. Do you know what this code does, or what you're trying to do?
Starting on the outside, then working inside: parseInt(str, base) will convert a String str into an integer, using base as base system. For example, parseInt('1500', 10) will result in the output 1500. str.padStart(n, padding) is a function that you call on a String str to add a string padding before it, the character padding, until it reaches the length n. For example, '150'.padStart(5, 'a') will result in aa1500.
What you are doing in that line is attempting to add padding to a number 10. Because 10 is already a number, this will fail because a number does not have the function padStart to use. If you were to use padStart for something, it should have been on your string, for example on msg.payload. I'm not saying that this is a good solution, I'm just explaining why your code is not working.