# Switch node with HEX input

**URL:** <https://discourse.nodered.org/t/switch-node-with-hex-input/19361>\
**Category:** General\
**Created:** [19 December 2019 19:00 UTC](https://discourse.nodered.org/t/switch-node-with-hex-input/19361 "2019-12-19T19:00:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [19 December 2019 19:00 UTC](https://discourse.nodered.org/t/switch-node-with-hex-input/19361/1 "2019-12-19T19:00:25Z")

</div>

Hi

I am new to NodeRED and I am trying to use the switcher node to filter a HEX code coming from a TCP in node

I receive this HEX 0x1c,0x64,0xa,0x0,0x0,0x0,0xff,0x77 I'd like to split the code up in to 8  
separate numbers, so I can react on each of the numbers.

 ![Udklip](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f62c5b0316e032a483cebf7aecd240e2d2f475ad.jpeg)

so the switch should work like this.

 ![Udklip3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c1b909cd7e025fd5d2a05a9d8d4c691e402e91c6.jpeg)

---

<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:** [19 December 2019 20:38 UTC](https://discourse.nodered.org/t/switch-node-with-hex-input/19361/2 "2019-12-19T20:38:20Z")

</div>

Hi - the switch node is generally for comparisons, not in the way you are thinking.  
You have asked it to match the input to a long string - "if no.0 ...." which of course it won't match.  
Realistically for multiple matches in a single go you are probably better off using a function node a small piece of javascript.

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [19 December 2019 20:46 UTC](https://discourse.nodered.org/t/switch-node-with-hex-input/19361/3 "2019-12-19T20:46:39Z")

</div>

Thanks

Can you help me setting up the function node,

How about using the switch like this, but i cant get it working

 ![Udklip4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3c1fd0450490ce67342364e54715b13852403238.jpeg)

---

<div class="post-metadata">

**Author:** ![zaher](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zaher/32/14891_2.png) [@zaher](https://discourse.nodered.org/u/zaher)\
**Post date:** [19 December 2019 20:49 UTC](https://discourse.nodered.org/t/switch-node-with-hex-input/19361/4 "2019-12-19T20:49:29Z")

</div>

I am not sure why you are using the Switcher node, You can use the **function node** to access each of the elements in your buffer array. For example, replace the Switcher node with a function node and add the following:

```auto
if (msg.payload[0] == 0x1c && msg.payload[1] == 0x64)
return msg;

```

This will filter the messages if I understood what you're trying to do.

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [19 December 2019 21:10 UTC](https://discourse.nodered.org/t/switch-node-with-hex-input/19361/5 "2019-12-19T21:10:02Z")

</div>

Thanks

if i then make two outputs, so i one condition is met go to output 1 and if another is met output 2

```auto
if (msg.payload[0] == 0x1c && msg.payload[1] == 0x64)
return msg; //Output 1
if (msg.payload[0] == 0x1c && msg.payload[1] == 0x63)
return msg; //Output 2

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [19 December 2019 21:13 UTC](https://discourse.nodered.org/t/switch-node-with-hex-input/19361/6 "2019-12-19T21:13:56Z")

</div>

You can do it with switches if prepared to calculate the numbers in decimal

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

```auto
[{"id":"cc1275cb.344bd8","type":"inject","z":"55400b8b.4f5704","name":"","topic":"","payload":"[28,100,65,66,67]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":100,"wires":[["9bbc9359.2faf8"]]},{"id":"9bbc9359.2faf8","type":"switch","z":"55400b8b.4f5704","name":"","property":"payload[0]","propertyType":"msg","rules":[{"t":"eq","v":"28","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":290,"y":140,"wires":[["f23fdff5.4782"]]},{"id":"177cf4e.6e5b80b","type":"debug","z":"55400b8b.4f5704","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":140,"wires":[]},{"id":"f23fdff5.4782","type":"switch","z":"55400b8b.4f5704","name":"","property":"payload[1]","propertyType":"msg","rules":[{"t":"eq","v":"100","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":140,"wires":[["177cf4e.6e5b80b"]]},{"id":"3a51dfd5.10543","type":"inject","z":"55400b8b.4f5704","name":"","topic":"","payload":"[28,99,65,66,67]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":160,"wires":[["9bbc9359.2faf8"]]}]

```

---

<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:** [19 December 2019 21:18 UTC](https://discourse.nodered.org/t/switch-node-with-hex-input/19361/7 "2019-12-19T21:18:39Z")

</div>

For multiple outputs from a function node see the docs  
[https://nodered.org/docs/user-guide/writing-functions#multiple-outputs](https://nodered.org/docs/user-guide/writing-functions#multiple-outputs)  
and increase the number of outputs to 2 (or whatever you need)

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [19 December 2019 21:21 UTC](https://discourse.nodered.org/t/switch-node-with-hex-input/19361/8 "2019-12-19T21:21:36Z")

</div>

Great thanks
