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.
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;
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.