# Decimal to Binary signed 2's complement

**URL:** <https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834>\
**Category:** General\
**Created:** [18 May 2021 12:09 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834 "2021-05-18T12:09:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [18 May 2021 12:09 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/1 "2021-05-18T12:09:36Z")

</div>

Hello,

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

Best Regards  
Aivaras

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 May 2021 12:21 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/2 "2021-05-18T12:21:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [18 May 2021 12:25 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/3 "2021-05-18T12:25:46Z")

</div>

Hello Steve,

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 May 2021 12:29 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/4 "2021-05-18T12:29:04Z")

</div>

then that is a simple javascript thing...

see number [.toString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bc0ee11c0d2d628db78b793e262d242d645511eb.png)

If necessary, you can pad the string with zeros out to 16 bits using [padStart](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/315b83d7a99849ddf45072bfdceb9af41436cbd9.png)

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [18 May 2021 12:34 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/5 "2021-05-18T12:34:20Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [18 May 2021 12:35 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/6 "2021-05-18T12:35:13Z")

</div>

Input is a string '3328'

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [18 May 2021 12:35 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/7 "2021-05-18T12:35:46Z")

</div>

\*input is a string "3328"

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 May 2021 12:36 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/8 "2021-05-18T12:36:19Z")

</div>

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](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

  
  

> [@Aivaras](#):
>
> Maybe you can repair this code ?

> [@Aivaras](#):
>
> Input is a string '3328'

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

```

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [18 May 2021 12:38 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/9 "2021-05-18T12:38:20Z")

</div>

Thank you very much Steve.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 June 2021 12:38 UTC](https://discourse.nodered.org/t/decimal-to-binary-signed-2s-complement/45834/10 "2021-06-01T12:38:41Z")

</div>

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