Convert Array of byte to utf8 string

Hey there,

I'm trying to get a string out of an array of bytes [127]. This is stored in a plc. I can translate the array to a readable string, but the string is now 128 in length (normally it should be something around 17 in UTF8).
However, now I can no longer work with this string because I want to open a file with the string.

I have now tried all the usual conversions that can be found in the network, but unfortunately none of them lead to success. Can anyone help me out?

image

Have you tried buffer-parser node. It is designed specifically for plc/modbus type byte data conversions.

yes I tried this node but it does not work (or I don t understand correctly)

in the picture, above is what i get from the opc ua node. below is what the buffer-parser brings out.
image

I dont understand why I can convert it into a string but not into a utf8 string.

const jsonBytesToString = String.fromCharCode(...msg.payload)

What is in the msg going into buffer-parser?

Add a debug node (set to show complete message) BEFORE the buffer parser,
image

and do the following...

  1. copy the content of the msg using the copy button that appears in the sidebar when you hover over the message. Paste this in a reply
    image

  2. expand all properties of the message and provide a screen shot so we can see what is in the msg and its sub properties.

{"_msgid":"b4186090.f55ff","payload":{"Name":"\u0000N\u0000\u0000\u0000u\u0000\u0000\u0000r\u0000\u0000\u0000 \u0000\u0000"},"topic":"ns=3;s=\"DB_Makro_01\".\"MakroName\"","datatype":"UInt8","statusCode":{"value":0},"originalPayload":[78,0,117,0,114,0,32,0,83,0,99,0,104,0,108,0,105,0,101,0,115,0,115,0,101,0,110,0,46,0,109,0,97,0,107,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0],"specification":{"options":{"byteSwap":[],"resultType":"keyvalue","singleResult":true,"msgProperty":"payload","setTopic":true},"items":[{"type":"string","name":"Name","offset":0,"length":16,"offsetbit":0,"scale":"1","mask":"","id":0,"value":"\u0000N\u0000\u0000\u0000u\u0000\u0000\u0000r\u0000\u0000\u0000 \u0000\u0000"}]},"values":["\u0000N\u0000\u0000\u0000u\u0000\u0000\u0000r\u0000\u0000\u0000 \u0000\u0000"],"objectResults":{"Name":{"type":"string","name":"Name","offset":0,"length":16,"offsetbit":0,"scale":"1","mask":"","id":0,"value":"\u0000N\u0000\u0000\u0000u\u0000\u0000\u0000r\u0000\u0000\u0000 \u0000\u0000"}},"keyvalues":{"Name":"\u0000N\u0000\u0000\u0000u\u0000\u0000\u0000r\u0000\u0000\u0000 \u0000\u0000"},"arrayResults":[{"type":"string","name":"Name","offset":0,"length":16,"offsetbit":0,"scale":"1","mask":"","id":0,"value":"\u0000N\u0000\u0000\u0000u\u0000\u0000\u0000r\u0000\u0000\u0000 \u0000\u0000"}],"buffer":[0,78,0,0,0,117,0,0,0,114,0,0,0,32,0,0,0,83,0,0,0,99,0,0,0,104,0,0,0,108,0,0,0,105,0,0,0,101,0,0,0,115,0,0,0,115,0,0,0,101,0,0,0,110,0,0,0,46,0,0,0,109,0,0,0,97,0,0,0,107,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0,0,32,0,0]}


That msg appears to be taken AFTER the buffer parser (it has things in it i recognise). Capture the msg going into the buffer parser.

Never mind, I got the buffer data from that message.

So the issue is your data is padded with spaces. you need to trim the string.

[{"id":"abbe56e5.fa6258","type":"inject","z":"d8bbcf2.f79033","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1100,"y":120,"wires":[["50188239.6cd26c"]]},{"id":"50188239.6cd26c","type":"function","z":"d8bbcf2.f79033","name":"your data","func":"\nreturn {\n    \"_msgid\":\"b4186090.f55ff\",\n    \"payload\":Buffer.from([78,0,117,0,114,0,32,0,83,0,99,0,104,0,108,0,105,0,101,0,115,0,115,0,101,0,110,0,46,0,109,0,97,0,107,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0,32,0])\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1260,"y":120,"wires":[["75a213c7.96c1dc"]]},{"id":"75a213c7.96c1dc","type":"buffer-parser","z":"d8bbcf2.f79033","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"utf16le","name":"Name","offset":0,"length":128,"offsetbit":0,"mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"setTopic":true,"x":1110,"y":180,"wires":[["bccaea5d.a012e8"]]},{"id":"77766320.44e09c","type":"debug","z":"d8bbcf2.f79033","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1430,"y":180,"wires":[]},{"id":"bccaea5d.a012e8","type":"function","z":"d8bbcf2.f79033","name":"trim","func":"msg.payload = msg.payload.Name.trim()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1270,"y":180,"wires":[["77766320.44e09c"]]}]

okay your data works perfect :slight_smile:
thank you very much!

I think the important part is this: msg.payload = msg.payload.Name.trim(); right?

That AND using utf16 le to decode the 16 bit data.

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