# Cut Strings and convert to signed int

**URL:** <https://discourse.nodered.org/t/cut-strings-and-convert-to-signed-int/32734>\
**Category:** Developing Nodes\
**Created:** [11 September 2020 07:48 UTC](https://discourse.nodered.org/t/cut-strings-and-convert-to-signed-int/32734 "2020-09-11T07:48:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SalazarSlytherin](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@SalazarSlytherin](https://discourse.nodered.org/u/SalazarSlytherin)\
**Post date:** [11 September 2020 07:48 UTC](https://discourse.nodered.org/t/cut-strings-and-convert-to-signed-int/32734/1 "2020-09-11T07:48:22Z")

</div>

Hey guys,

i am completely new to node red and i don't find a perfect way to cut my strings.  
I finally managed to get my data per MQTT to node red but know i don't know how to go further.

The string i get for example is this:

{"data":"0167EC0002685F","datetime":"2020-09-11T09:18:44Z","devaddr":"006E1453","fcnt":8}

So what i need are the digits behind 67 :"EC00"

After that i have to swap them to this: 00EC

And then i need to convert that hex value to an signed integer value...

Thanks in advance

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 September 2020 08:00 UTC](https://discourse.nodered.org/t/cut-strings-and-convert-to-signed-int/32734/2 "2020-09-11T08:00:44Z")

</div>

Hi @SalazarSlytherin

welcome to the forum.

It looks like the string you have is a valid JSON string. That means you can configure the MQTT In node to output a "parsed JSON Object" - that will convert the string to an object which will make it easier to access the individual properties.

Once you've done that, you'll be able to access the value you want with `msg.payload.data`.

In the example you've shared, you want to swap `EC00` for `00EC`. Is that the exact change you always want to make? Or is it a more general requirement to swap the 5/6th characters with the 7/8th bytes?

For a general swap of those bytes, you could do:

```auto
msg.payload.data = msg.payload.data.replace(/^(....)(..)(..)(.*)/,"$1$3$2$4")

```

If you want to always swap `EC00` for `00EC` in those positions, you could do:

```auto
msg.payload.data = msg.payload.data.replace(/^(....)EC00(.*)/,"$00EC1$2")

```

Then to convert that to an int:

```auto
var intValue = parseInt(msg.payload.data, 16);
```

---

<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:** [1 October 2020 08:09 UTC](https://discourse.nodered.org/t/cut-strings-and-convert-to-signed-int/32734/3 "2020-10-01T08:09:14Z")

</div>

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