# How to convert a string

**URL:** <https://discourse.nodered.org/t/how-to-convert-a-string/74927>\
**Category:** General\
**Tags:** modbus\
**Created:** [9 February 2023 03:49 UTC](https://discourse.nodered.org/t/how-to-convert-a-string/74927 "2023-02-09T03:49:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asdrubouscaras](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@asdrubouscaras](https://discourse.nodered.org/u/asdrubouscaras)\
**Post date:** [9 February 2023 03:49 UTC](https://discourse.nodered.org/t/how-to-convert-a-string/74927/1 "2023-02-09T03:49:41Z")

</div>

Hi,

I have a Novus device which has RS-485 communication and works as a modbus gateway.  
It is possible to read/write to slave devices connected to the gateway over MQTT publish.  
The device return in another topic a payload containing the modbus response in HEX, but the result comes as a string.

![string 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e740be183fc3730a5bed761d2a2bfd23e879fc5.png)

What I want is the value on the requested register.

![string result](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3cd41b55a081631125603cae7b4a87958164b714.png)

I've tried to follow some tutorials on how to parse modbus values, but lead me to nowhere.

how can I convert this string to the real value of the register?

Sorry if this is a repeated topic, But I didn't find anything related to this.

I would appreciate any help.

Thanks.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [9 February 2023 13:51 UTC](https://discourse.nodered.org/t/how-to-convert-a-string/74927/2 "2023-02-09T13:51:03Z")

</div>

Hello .. you can try in a Function node

```auto
// remove spaces from string
let buf = msg.payload.replaceAll(" ", "");
node.warn(buf)

// convert string to Buffer
buf = Buffer.from(buf, "hex")

// read Int from offset 
msg.payload = buf.readInt16BE(3);
return msg;

```

**Example flow :**

```auto
[{"id":"8b3e824f6868e38a","type":"inject","z":"937cb040.d8e16","name":"hex string","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"03 03 04 01 2D 00 00 48 06","payloadType":"str","x":380,"y":640,"wires":[["38c5be1a1f276787"]]},{"id":"38c5be1a1f276787","type":"function","z":"937cb040.d8e16","name":"function","func":"// remove spaces from string\nlet buf = msg.payload.replaceAll(\" \", \"\");\nnode.warn(buf)\n\n// convert string to Buffer\nbuf = Buffer.from(buf, \"hex\")\n\n// read Int from offset \nmsg.payload = buf.readInt16BE(3);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":640,"wires":[["6ef999e4341e5a25"]]},{"id":"6ef999e4341e5a25","type":"debug","z":"937cb040.d8e16","name":"debug 26","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":640,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![asdrubouscaras](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@asdrubouscaras](https://discourse.nodered.org/u/asdrubouscaras)\
**Post date:** [10 February 2023 04:40 UTC](https://discourse.nodered.org/t/how-to-convert-a-string/74927/3 "2023-02-10T04:40:02Z")

</div>

Hey @UnborN, you helped me so much.

Your solution worked.  
I was able to combine your function with another piece of code that I had.  
Now I can have both address and value on the payload.

 ![function funcional](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a32127eb2ad4208927399bd3bcb905110e6c411.png)

![debug payload](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/4505e279733b49c91aeeecbc287780bb3c3b8147.png)

I know that is probably an ugly workaround, but works for me at the moment.. 🙂

Thank you, man.  
You rock!

---

<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:** [11 April 2023 04:40 UTC](https://discourse.nodered.org/t/how-to-convert-a-string/74927/4 "2023-04-11T04:40:38Z")

</div>

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