# How do I simulate a hexadecimal input to be parsed to string

**URL:** <https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228>\
**Category:** General\
**Created:** [17 September 2018 21:23 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228 "2018-09-17T21:23:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![quique123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/quique123/32/2630_2.png) [@quique123](https://discourse.nodered.org/u/quique123)\
**Post date:** [17 September 2018 21:23 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228/1 "2018-09-17T21:23:01Z")

</div>

I am trying to simulate data from a ble device receiving hex data. I need to parse it and display the string. How do I simulate that in node-red?

So far I have an inject node, function node and debug node, but what do I make the inject node in order to simulate 0x65 for example?

---

<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:** [17 September 2018 21:29 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228/2 "2018-09-17T21:29:40Z")

</div>

Do you mean a two char string "65" a four char string "0x65" or a byte represented in hex by 0x65 (decimal 101)?

---

<div class="post-metadata">

**Author:** ![quique123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/quique123/32/2630_2.png) [@quique123](https://discourse.nodered.org/u/quique123)\
**Post date:** [17 September 2018 21:32 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228/3 "2018-09-17T21:32:06Z")

</div>

No, my relay board takes for example command 0x65, which i understand is hexadecimal. I need to convert it to its string form of "e".

So yes, the byte in hex represented by 0x65

---

<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:** [17 September 2018 21:44 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228/4 "2018-09-17T21:44:43Z")

</div>

This will send a single byte of value (decimal) 101

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

---

<div class="post-metadata">

**Author:** ![quique123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/quique123/32/2630_2.png) [@quique123](https://discourse.nodered.org/u/quique123)\
**Post date:** [17 September 2018 21:50 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228/5 "2018-09-17T21:50:35Z")

</div>

ok but then im using the function node with msg.payload.toString() but I get this error:  
"Function tried to send a message of type string"

```
// Create a Date object from the payload

```

var resultado = msg.payload.toString();

// Return the message so it can be sent on  
return resultado;

---

<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:** [17 September 2018 22:15 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228/6 "2018-09-17T22:15:03Z")

</div>

I think this is what you are after to convert it to an "e"

```
msg.payload = msg.payload.toString('utf8');
return msg;
```

---

<div class="post-metadata">

**Author:** ![quique123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/quique123/32/2630_2.png) [@quique123](https://discourse.nodered.org/u/quique123)\
**Post date:** [18 September 2018 14:36 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228/7 "2018-09-18T14:36:36Z")

</div>

ok thanks. For future reference (of mine) in the function I had this code:

```
// Create a Date object from the payload
var resultado = msg.payload.toString('utf8');

// Return the message so it can be sent on
return resultado;

```

which effectively took the value from msg and put it into resultado, returning resultado. This gave an error:

"Function tried to send a message of type string"

because what is passed over to the debug node, is msg.payload. Is there a way to pass resultado to the debug node and manipulate it over there? Or do I always have to replace the msg variable?

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [18 September 2018 15:04 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228/8 "2018-09-18T15:04:47Z")

</div>

Function nodes must always return a Javascript object -- your code pulls the payload value (a buffer of 1 character) out of the incoming `msg` object, converts it to a string, and tries to return a bare string. This is not allowed, since the downstream nodes always expect to receive a msg object.

The best practice would be to reuse the incoming msg object, but replace the payload field:

```auto
// replace the payload buffer with the string value
msg.payload = msg.payload.toString('utf8');
// return the original msg with the new string payload
return msg;

```

This practice ensures that any other properties (or fields) of the incoming msg (e.g. topic, parts, msgid, req, res, etc) are not destroyed -- which can cause things like the `join` and `http response` nodes to not work.

Also, simulating buffer input in an `inject` node is pretty simple, since there is a buffer option. If you click the `...` next to the input field, it opens a buffer editor panel which shows the hex values of each character as you type:

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

Your case is a bit different, since you know the hex value -- in this case, you can type each hex string into the field directly, as long as you use brackets to denote an array, double quotes around each hex "string", and separate each hex string with a comma, like so:

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

_Whew!_ -- ugly, but it works...

**NR Team** : are there any plans to enhance the Buffer input field to accept valid syntax for JS arrays? Right now if I type "hello" in the buffer editor, then the field value is set to `[104,101,108,108,111]` (the ascii equivalent). But the alternate hex (or octal, or binary) forms like `[0x68,0x65,0x6C,0x6C,0x6F]` are treated as a plain string.

---

<div class="post-metadata">

**Author:** ![Maco65](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Maco65](https://discourse.nodered.org/u/Maco65)\
**Post date:** [10 March 2020 20:06 UTC](https://discourse.nodered.org/t/how-do-i-simulate-a-hexadecimal-input-to-be-parsed-to-string/3228/9 "2020-03-10T20:06:16Z")

</div>

@shrickus - thank you. This has helped my in my Node-RED project to send some check data to serial interface from Inject node !
