Convert Object array of SINT's to String

Hi all,

Looking for some help, Using the Node Red Ethernet IP node I have some data coming in the form of an object of SINTs. This is variable length. I want to use a function node to convert this into a String. I've tried a few things but not having much success.

image

Why? You can send it through a json node and that will convert it to a JSON string. You may need to give an example of how you wish this string formatted, if it's not a JSON string you require.

To convert these char values to ascii characters you will need to...

  • convert the data into a Buffer
  • call Buffer.toString.

This function will do it for you.

const values = Object.values(msg.payload)
const buf = Buffer.alloc(values.length)
let pos = 0;
for (let index = 0; index < values.length; index++) {
    const val = values[index];
    if (val <= 0) break
    pos = buf.writeUInt8(val,pos);
}
msg.payload = buf.slice(0,pos).toString()
return msg;

image

WARNING:

There is no guarantee an Objects keys will be evaluated in the order you expect. You might have to manipulate the keys and sort the data by the number inside the [square brackets]

Is that exactly how it is coming in? It seems a very strange format. If you are starting with something different and then converting it to that then please show us what you started with. We may be able to suggest a better strategy.

node-red-contrib-buffer-parser is a handy tool to create a buffer or take values from a buffer. Just a thought.

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