# How to obtain binary output with hex string

**URL:** <https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051>\
**Category:** General\
**Created:** [3 September 2019 10:37 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051 "2019-09-03T10:37:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [3 September 2019 10:37 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/1 "2019-09-03T10:37:54Z")

</div>

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

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

 ![help](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/003f5cd1f72292841782a693131d3a42f7cd97cf.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [3 September 2019 11:16 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/2 "2019-09-03T11:16:19Z")

</div>

> [@mike829](#):
>
> ''' msg.payload = msg.payload.split(' ')[1].substr(5,1);  
> parseInt(msg.payload.substr, 2)  
> return msg; '''

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?

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [3 September 2019 11:47 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/3 "2019-09-03T11:47:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [3 September 2019 11:50 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/4 "2019-09-03T11:50:32Z")

</div>

The page in the docs might be useful. [https://nodered.org/docs/user-guide/writing-functions](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.

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [3 September 2019 13:59 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/5 "2019-09-03T13:59:03Z")

</div>

It is so hard for me...

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

```

I will try...

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [3 September 2019 14:04 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/6 "2019-09-03T14:04:23Z")

</div>

where did the context value come from?

[https://www.w3schools.com/js/js\_numbers.asp](https://www.w3schools.com/js/js_numbers.asp)

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [3 September 2019 14:24 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/7 "2019-09-03T14:24:35Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [3 September 2019 15:10 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/8 "2019-09-03T15:10:04Z")

</div>

> [@mike829](#):
>
> '''

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

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [3 September 2019 15:24 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/9 "2019-09-03T15:24:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [3 September 2019 17:17 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/10 "2019-09-03T17:17:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [3 September 2019 17:39 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/11 "2019-09-03T17:39:04Z")

</div>

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);`

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 September 2019 19:45 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/12 "2019-09-03T19:45:09Z")

</div>

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](https://www.w3schools.com/js/js_bitwise.asp)  
eg

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

```

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [3 September 2019 20:15 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/13 "2019-09-03T20:15:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [4 September 2019 07:44 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/14 "2019-09-04T07:44:26Z")

</div>

Hi Guys,

thanks to you I did it and it works fine:

```auto
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

```auto

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?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [4 September 2019 07:52 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/15 "2019-09-04T07:52:33Z")

</div>

right yes it expects the input to be a number

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [4 September 2019 08:39 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/16 "2019-09-04T08:39:39Z")

</div>

Ok, so it is wrong:

```auto
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?

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [4 September 2019 08:48 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/17 "2019-09-04T08:48:57Z")

</div>

> [@mike829](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [4 September 2019 09:51 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/18 "2019-09-04T09:51:19Z")

</div>

Sorry...  
I asked because I converted using

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

```

but I receive 100 instead 0100 and I tryed to put

```auto
.padStart(4, '0');

```

but the below code doesn't work:

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [4 September 2019 10:01 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/19 "2019-09-04T10:01:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [4 September 2019 10:05 UTC](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051/20 "2019-09-04T10:05:09Z")

</div>

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.

> [@mike829](#):
>
> `msg.payload = parseInt(msg.payload, (10).padStart(4, '0'));`

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.

[Next page](https://discourse.nodered.org/t/how-to-obtain-binary-output-with-hex-string/15051.md?page=2)
