# Convert Array of byte to utf8 string

**URL:** <https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223>\
**Category:** General\
**Created:** [18 February 2021 09:39 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223 "2021-02-18T09:39:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![YT2310](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yt2310/32/35255_2.png) [@YT2310](https://discourse.nodered.org/u/YT2310)\
**Post date:** [18 February 2021 09:39 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223/1 "2021-02-18T09:39:14Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27da686cb58677d08a1a6bef9eb9eb07c4e12398.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 February 2021 10:28 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223/2 "2021-02-18T10:28:23Z")

</div>

Have you tried [buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser) node. It is designed specifically for plc/modbus type byte data conversions.

---

<div class="post-metadata">

**Author:** ![YT2310](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yt2310/32/35255_2.png) [@YT2310](https://discourse.nodered.org/u/YT2310)\
**Post date:** [18 February 2021 10:54 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223/3 "2021-02-18T10:54:15Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2b1a8f77357648666462d54313b1338c4fcd1b93.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c08ab89eae974c3506716338c50f94d74dfe7dbf.png)

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

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

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 February 2021 10:58 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223/4 "2021-02-18T10:58:46Z")

</div>

What is in the msg going into buffer-parser?

Add a debug node (set to show complete message) BEFORE the buffer parser,  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/780fa53620481b4e5d4c8134bb68d44af2bae3ee.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2c30c136b014308fd320cce04eee4e6fe7c639e6.png)

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.

---

<div class="post-metadata">

**Author:** ![YT2310](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yt2310/32/35255_2.png) [@YT2310](https://discourse.nodered.org/u/YT2310)\
**Post date:** [18 February 2021 11:53 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223/5 "2021-02-18T11:53:52Z")

</div>

```auto
{"_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]}

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c9c436f1fb69d9993265e071a941b395f6c674a7.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/18c768bc2b8e1d3143cf772faa3811c6568dbe03.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 February 2021 11:56 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223/6 "2021-02-18T11:56:53Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/ba358e9f221d62ecd31865340bc2b2e36fc80610.png)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![YT2310](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yt2310/32/35255_2.png) [@YT2310](https://discourse.nodered.org/u/YT2310)\
**Post date:** [18 February 2021 12:47 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223/7 "2021-02-18T12:47:45Z")

</div>

okay your data works perfect 🙂  
thank you very much!

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 February 2021 12:50 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223/8 "2021-02-18T12:50:31Z")

</div>

> [@YT2310](#):
>
> 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.

---

<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:** [4 March 2021 12:51 UTC](https://discourse.nodered.org/t/convert-array-of-byte-to-utf8-string/41223/9 "2021-03-04T12:51:32Z")

</div>

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