Decimal to Binary signed 2's complement

Hello,

Could someone help me to make a Decimal to Binary signed 2's complement converter in node-red ?

Best Regards
Aivaras

Hi.

what exactly do you want?

Supposing your number was 65535 - do you want "0xFFFF" or 1111111111111111 or an array of bools or array of ones and zeroes?

Hello Steve,

I have decimal number 3328 and I want it like this 0000110100000000.

then that is a simple javascript thing...

see number .toString

image

If necessary, you can pad the string with zeros out to 16 bits using padStart

image

Thank you for info, but I am a new at javascript.
Maybe you can repair this code ?
input = msg.payload;

OCT = parseInt(input, 16).toString(2);

msg.payload = OCT;

return msg;

Input is a string '3328'

*input is a string "3328"

Firstly, in order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```

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



var input = Number(msg.payload);
msg.payload = input.toString(2).padStart(16,"0") ;
return msg;

Thank you very much Steve.

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