How to obtain binary output with hex string

Hello Everybody,

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?

Thanks in advance.

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?

Sorry, i am "learning by doing", so I am doing some error. You're right.

So, I've to ad new function box, declare msg.payload as hex and then convert it to bin...

I will try...

Thanks for your precious help

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.

It is so hard for me...

myvar = (16);
parseInt(myvar, 2);
return msg; 

I will try...

where did the context value come from?

https://www.w3schools.com/js/js_numbers.asp

myvar = myvar.toString(16);
parseInt(myvar, 2);
return msg;

Tip: If you can't find the backtick (```) on your keyboard for code formatting, try to use the </> button visible in the post editor.

Again,

Node-RED works with the msg object
That’s what you are passing to the next node.

You need to ensure the transformations you make end up part of the msg.object if you want it passes to the next node.

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

If it's a Hex number why are you trying to convert it TO Hex?

You need to declare that it's Hex. How to declare its a HEX value is shown in the link I gave you js_numbers...

msg.payload = parseInt("0x"+msg.payload);

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}];

Thanks guys, tomorrow I will try... and will let you know

Hi Guys,

thanks to you I did it and it works fine:

msg.payload = msg.payload.toString(2).padStart(4, '0');
return msg;

The last question is how to connect the n.4 function box outputs from var m0 to var m3:

I put the code


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}];

but I obtain always 0000. Is it possible it doesn't work because binary output is a string?

right yes it expects the input to be a number

Ok, so it is wrong:

msg.payload = msg.payload.toString(2).padStart(4, '0');

How can I covert to bin as numbers? Or I need to convert the string to numer? Sorry... Can you help me?

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.

Sorry...
I asked because I converted using

msg.payload = parseInt(msg.payload, (10));

but I receive 100 instead 0100 and I tryed to put

.padStart(4, '0');

but the below code doesn't work:

msg.payload = parseInt(msg.payload, (10).padStart(4, '0'));

I tryed to search on google and here but I don't know how to apply what I found to my project.

Just use parseInt(msg.payload) without the ,2. Padding it will make it a string again

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.