# String Decode Function

**URL:** <https://discourse.nodered.org/t/string-decode-function/27464>\
**Category:** General\
**Created:** [28 May 2020 22:14 UTC](https://discourse.nodered.org/t/string-decode-function/27464 "2020-05-28T22:14:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rromele](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rromele/32/18431_2.png) [@rromele](https://discourse.nodered.org/u/rromele)\
**Post date:** [28 May 2020 22:14 UTC](https://discourse.nodered.org/t/string-decode-function/27464/1 "2020-05-28T22:14:05Z")

</div>

On the serial I always receive this string in the same format:

![ast-nr2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/4198d3a41d4be7cd4dcd6f07aa570f626c010aa7.jpeg)

I should break it down according to the native format:

 ![AST-NR1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f5782cc1afcecfceb464213331669735138c07a.jpeg)

What could be the best way to do it?

Thank you so much for your help.

R.

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [29 May 2020 01:03 UTC](https://discourse.nodered.org/t/string-decode-function/27464/2 "2020-05-29T01:03:49Z")

</div>

In a function node with **substring** should do the trick if the length is always the same.

---

<div class="post-metadata">

**Author:** ![rromele](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rromele/32/18431_2.png) [@rromele](https://discourse.nodered.org/u/rromele)\
**Post date:** [29 May 2020 21:08 UTC](https://discourse.nodered.org/t/string-decode-function/27464/3 "2020-05-29T21:08:49Z")

</div>

thanks, do you have a small example to start working on?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 May 2020 22:58 UTC](https://discourse.nodered.org/t/string-decode-function/27464/4 "2020-05-29T22:58:48Z")

</div>

This should give you a start.

Basically extract the posts from the string in a function node, use parseInt were necessary to convert strings to numbers (search JavaScript parseInt for help on converting hex string parts to numbers)

```auto
var data = msg.payload;
var pl ={};//make new empty object

//Now populate the object with values
pl.data = data;//keep copy of original data. Handy for debugging
pl.stnNo = parseInt(data.substr(1,2));
pl.cmd = data.substr(3,2);
//... And so on

// Set payload to our new object
msg.payload = pl;
return msg;//return it to next node

```

So put that ^ in a function node, fill out the rest of the `pl` object properties, add a debug node & check it's all correctly split up.

---

<div class="post-metadata">

**Author:** ![rromele](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rromele/32/18431_2.png) [@rromele](https://discourse.nodered.org/u/rromele)\
**Post date:** [30 May 2020 23:01 UTC](https://discourse.nodered.org/t/string-decode-function/27464/5 "2020-05-30T23:01:15Z")

</div>

@Steve-Mcl ,  
thank you so much for your help, that's just what i needed.

 ![pir](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/0388206e53633aee4ed881b0a64a47a2e428326b.jpeg)

---

<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:** [13 June 2020 23:01 UTC](https://discourse.nodered.org/t/string-decode-function/27464/6 "2020-06-13T23:01:16Z")

</div>

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