# Lack of a proper padding in a binary conversion

**URL:** <https://discourse.nodered.org/t/lack-of-a-proper-padding-in-a-binary-conversion/70124>\
**Category:** General\
**Created:** [5 November 2022 15:49 UTC](https://discourse.nodered.org/t/lack-of-a-proper-padding-in-a-binary-conversion/70124 "2022-11-05T15:49:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lobster87](https://avatars.discourse-cdn.com/v4/letter/l/8491ac/32.png) [@lobster87](https://discourse.nodered.org/u/lobster87)\
**Post date:** [5 November 2022 15:49 UTC](https://discourse.nodered.org/t/lack-of-a-proper-padding-in-a-binary-conversion/70124/1 "2022-11-05T15:49:53Z")

</div>

Hi,

I try to convert hex to binary using this code:

```auto
var pattern='5c48a02b8c00';
var pattern_bits ="";
msg.payload=parseInt("0x"+pattern).toString(2).padStart(4, '0');
return msg;

```

It returns the string: **10111000100100010100000001010111000110000000000**. I have checked this result in online calc, and the result is:_010111000100100010100000001010111000110000000000_\* The difference is in the first zero from the left. Probably it is because 5 is **101**. But based on code **101** should be padded to four characters ( **0101** ). What is wrong?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 November 2022 16:14 UTC](https://discourse.nodered.org/t/lack-of-a-proper-padding-in-a-binary-conversion/70124/2 "2022-11-05T16:14:39Z")

</div>

You need `padStart(48, '0')`.  
You want 48 bits total.  
Numerically there is no difference of course.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 November 2022 16:16 UTC](https://discourse.nodered.org/t/lack-of-a-proper-padding-in-a-binary-conversion/70124/3 "2022-11-05T16:16:04Z")

</div>

What is it that you want to do with the result?

---

<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:** [4 January 2023 16:16 UTC](https://discourse.nodered.org/t/lack-of-a-proper-padding-in-a-binary-conversion/70124/4 "2023-01-04T16:16:13Z")

</div>

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