Cut Strings and convert to signed int

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

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:

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:

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

Then to convert that to an int:

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

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