# How to convert array byte to ASCII

**URL:** <https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607>\
**Category:** General\
**Created:** [9 January 2019 10:48 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607 "2019-01-09T10:48:59Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [18 March 2020 23:04 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/21 "2020-03-18T23:04:23Z")

</div>

Hi Colin,

I did a test... comparing the output received via Node-Red and directly from the inverter monitoring app. Here's what I found...

Just focusing on KDY which is Energy today [Wh]. The inverter monitoring application showed...

![InverterStats](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34c829bc25f05da6738f17fbcfc3b0ef316677b8.jpeg)

Then I ran the Node-Red data retrieve function and got the following from the inverter...

{01;FB;29|64:KDY=5;KT0=5317;PAC=800|08BB}

**EDIT** : I also noticed a correlation between PAC=800 and the inverter monitoring app, so the numbers seem to line up, just I think the hexadecimal may be getting in the way.

Focusing on just KDY, if I look at the KDY=5 then it appears to correlate with the inverter monitoring app, although the formatting isn't the same i.e. 5 versus 0.5.

In your opinion, would it be better to try and convert the KDY=?? and KT0=?? to pure decimal, so I am not getting caught by hexadecimal oddities? After that, I can then focus on comparing the numbers between the Node-Red data retrieval and the inverter monitoring app.

Thanks again, Mark

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 March 2020 08:57 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/22 "2020-03-19T08:57:14Z")

</div>

OK, it seems you just have to convert the hex to decimal and divide by 10. The first thing then is to extract the values from the string. Looks like the easiest thing is first to splt on the `|` characters, select the second part, and then split on the `:`, again take the second part then split on the `;` to get the three parts KDY=5, KT0=5317 and PAC=800. Then each of those can be split on the `=` to get the individual values. Those can then be converted to decimal numbers and divided by ten. This is not the simplest bit of code so I have done it for you, but please do make sure you understand it and ask about bits you don't.

```auto
[{"id":"50c685f.3fe2f7c","type":"inject","z":"bdd7be38.d3b55","name":"test string","topic":"","payload":"{01;FB;29|64:KDY=5;KT0=5317;PAC=800|08BB}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":244,"y":69,"wires":[["758bea22.9f3d8c"]]},{"id":"758bea22.9f3d8c","type":"function","z":"bdd7be38.d3b55","name":"split","func":"let firstSplit = msg.payload.split(\"|\")\nlet centreSection = firstSplit[1]\nlet valueString = centreSection.split(\":\")[1]\nlet valuesArray = valueString.split(\";\")\nmsg.payload = {}\nfor (let i=0; i<valuesArray.length; i++) {\n let splitValue = valuesArray[i].split(\"=\")\n let key = splitValue[0] // this is \"KDY\" etc\n let hexValue = splitValue[1] // this is the hex string for the key\n let value = parseInt(hexValue,16) / 10 // convert to decimal and divide by 10\n // now add it to the payload\n msg.payload[key] = value\n}\nreturn msg;","outputs":1,"noerr":0,"x":383,"y":68,"wires":[["c888f4c0.f0fa98"]]},{"id":"c888f4c0.f0fa98","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":537,"y":68,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 March 2020 08:34 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/23 "2020-03-22T08:34:13Z")

</div>

@gregoinc did that work for you?

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [31 March 2020 21:23 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/24 "2020-03-31T21:23:05Z")

</div>

Hi Colin,

Apology for not replying. Unfortunately the pandemic has derailed my investigations, having to focus more time on my paying work than fun work 😀 due to my employer supporting various functions in Australia.

I intend to take a look this weekend time permitting. Hope you and your family are well and keeping ahead of things.

Regards, Mark

---

<div class="post-metadata">

**Author:** ![djvanas](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@djvanas](https://discourse.nodered.org/u/djvanas)\
**Post date:** [6 April 2020 20:45 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/25 "2020-04-06T20:45:26Z")

</div>

Hi All!

Greetings - hope you are well. I came across this topic whilst trying to capture two MQTT messages and output them through UDP message. The JSON looks like this;

{  
"id":"ztatz\_dt",  
"version":1.0,  
"t\_unit":"C",  
"t\_in\_avg":24.1,  
"t\_out\_avg":22.0,  
"t\_in":24.298,  
"t\_out":22.150,  
"seq":24464  
}

If I convert to ASCII, the data looks like:  
7B 0D 0A 22 69 64 22 3A 22 7A 74 61 74 7A 5F 64 74 22 2C 0D 0A 22 76 65 72 73 69 6F 6E 22 3A 31 2E 30 2C 0D 0A 22 74 5F 75 6E 69 74 22 3A 22 43 22 2C 0D 0A 22 74 5F 69 6E 5F 61 76 67 22 3A 32 34 2E 31 2C 0D 0A 22 74 5F 6F 75 74 5F 61 76 67 22 3A 32 32 2E 30 2C 0D 0A 22 74 5F 69 6E 22 3A 32 34 2E 32 39 38 2C 0D 0A 22 74 5F 6F 75 74 22 3A 32 32 2E 31 35 30 2C 0D 0A 22 73 65 71 22 3A 32 34 34 36 34 0D 0A 7D

So I receive t\_in and t\_out though MQTT on topics t\_in and t\_out. How can I convert the MQTT to ASCII (same as above) and +1 the seq number (till 32768, and the start over - begin with 1).

Hope someone can help - I've spent 10+ hours and it is driving me crazy....

David

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [13 April 2020 04:34 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/26 "2020-04-13T04:34:21Z")

</div>

Hi Colin,

Finally managed to find some time over Easter in daylight when the solar inverter was running. First question, how should the code be formatted, it appears to be just one lone line/string of characters, so I am wondering for me to understand what is happening would it be easier to break it down into chunks/sections? Or am I looking at it from the wrong angle?

Also, when I run the code you gave me I get the following output....

[123,48,49,59,70,66,59,50,65,124,54,52,58,75,68,89,61,54,48,59,75,84,48,61,53,51,69,65,59,80,65,67,61,66,52,54,124,48,57,50,54,125]

![script_output](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f62eeb35840ad80f0d133543c96ffa4f9ec83198.jpeg)

But I have no idea what the numbers mean?

Thanks, Mark

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 April 2020 07:45 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/27 "2020-04-13T07:45:36Z")

</div>

I don't understand why you are getting that output, when I click the inject node in my test flow I get this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/b/3befc74e31f165c4be408354c50a19dd5f944a3d.png)  
Do you get something different when you click the inject node?

There is absolutely no way the function node I posted could return the array you have shown. That debug node output is not coming from my function. Use separate debug nodes at each point in the flow and give the debug nodes names so you can see what output is coming from which node.

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [13 April 2020 22:12 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/28 "2020-04-13T22:12:11Z")

</div>

Hi Colin,

Most likely my lack of knowledge is causing the issues. I will attempt to pictorially show the setup below.

The function with your code is identified below as "Test Code" function...

 ![TestFlow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/9/b97dc4b1c1c0f935fe7f8f50aa22aabd8a530449.jpeg)

Inside the function injecting the query to the inverter...

 ![InjectNode](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13d9e1bb51b75d8170fa879bf46b38b57115f32e.jpeg)

Hope this helps. Let me know if you need more info.

Thanks, Mark

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [13 April 2020 22:13 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/29 "2020-04-13T22:13:37Z")

</div>

And... Inside the function containing your code...

 ![TestFunction](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9cdc3fd181d1e05989931141967af84d6aadba76.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 April 2020 06:15 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/30 "2020-04-14T06:15:21Z")

</div>

You appear to have pasted my whole flow into your function node. Instead you need to import it into node red. Mark and Copy it from my post then in Node red from the hamburger menu select import and paste from clipboard (I forget the exact phrase). Then it will appear in your flow. You can test the function using the inject node and then use the function in your flow.

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [14 April 2020 10:19 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/31 "2020-04-14T10:19:46Z")

</div>

> [@Colin](#):
>
> [{"id":"50c685f.3fe2f7c","type":"inject","z":"bdd7be38.d3b55","name":"test string","topic":"","payload":"{01;FB;29|64:KDY=5;KT0=5317;PAC=800|08BB}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":244,"y":69,"wires":[["758bea22.9f3d8c"]]},{"id":"758bea22.9f3d8c","type":"function","z":"bdd7be38.d3b55","name":"split","func":"let firstSplit = msg.payload.split("|")\nlet centreSection = firstSplit[1]\nlet valueString = centreSection.split(":")[1]\nlet valuesArray = valueString.split(";")\nmsg.payload = {}\nfor (let i=0; i\<valuesArray.length; i++) {\n let splitValue = valuesArray[i].split("=")\n let key = splitValue[0] // this is "KDY" etc\n let hexValue = splitValue[1] // this is the hex string for the key\n let value = parseInt(hexValue,16) / 10 // convert to decimal and divide by 10\n // now add it to the payload\n msg.payload[key] = value\n}\nreturn msg;","outputs":1,"noerr":0,"x":383,"y":68,"wires":[["c888f4c0.f0fa98"]]},{"id":"c888f4c0.f0fa98","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":537,"y":68,"wires":}]

Hi Colin,

Ok, I am a bit embarrassed... but I did say it would be me that would be causing the issues. I have imported the code successfully, tested the 'test string' which gave the correct output to debug. I have joined it to my input from the inverter, so when I have some sunshine tomorrow I will do a test input from the inverter. You have the patience of a saint.

Thanks, Mark

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [14 April 2020 22:15 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/32 "2020-04-14T22:15:59Z")

</div>

Hi Colin,

Did a test this morning... remotely from work. And when I click on 'inject' I get an error, but when I click on test string it works?

 ![firsstrun](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a7ac2f6f0f5405da48a5deec47a7c2a0d4eed93.jpeg)

Not sure why there's an error, and I am unable to understand why Node-Red is stating msg.payload.split is not a function?

Thanks, Mark

---

<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:** [14 April 2020 23:20 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/33 "2020-04-14T23:20:02Z")

</div>

> [@gregoinc](#):
>
> I am unable to understand why Node-Red is stating msg.payload.split is not a function?

Probably because `msg.payload` is not a string (probably null or undefined or perhaps something else like an object)

Put a debug node before where that error occurs - inspect the contents.

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [15 April 2020 03:13 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/34 "2020-04-15T03:13:24Z")

</div>

This is what is coming out of the SolarMax\_Connect connection...

[123,48,49,59,70,66,59,50,65,124,54,52,58,75,68,89,61,51,69,59,75,84,48,61,53,51,70,57,59,80,65,67,61,68,56,56,124,48,57,51,57,125]

After I run it through my convert to ASCII I get this...

{01;FB;2A|64:KDY=3F;KT0=53F9;PAC=B8A|0941}

Here's the screen capture...

 ![msg.payloadcapture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/875395936737d08f0997491b7859d748ae5bbb0a.jpeg)

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [15 April 2020 04:18 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/35 "2020-04-15T04:18:51Z")

</div>

I changed the config, basically placing split after Convert to ASCII and bingo it appears to work.

 ![NewConfig](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/501e05acb783eb18538974f5308292a5b1466a5f.jpeg)

No Errors... and the numbers look pretty good.

![GoodNumbers](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/ce334f523d4065673f9e377542b9ffa6203f6931.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 April 2020 06:11 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/36 "2020-04-15T06:11:42Z")

</div>

Yes, if you want a function to work then you have to give it the data that it is designed to expect.

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [16 April 2020 01:22 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/37 "2020-04-16T01:22:17Z")

</div>

Point taken, unfortunately my lack of experience with Node-Red is an obvious one 😀

Thanks again Colin, your advice and patience is very much appreciated.

---

<div class="post-metadata">

**Author:** ![steinerma](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steinerma/32/37666_2.png) [@steinerma](https://discourse.nodered.org/u/steinerma)\
**Post date:** [22 February 2021 11:33 UTC](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607/38 "2021-02-22T11:33:51Z")

</div>

Hi gregoinc  
could you post the flow for reading the Solarmax data and converting the ASCII code here? Would be a big help for me

[Previous page](https://discourse.nodered.org/t/how-to-convert-array-byte-to-ascii/6607.md?page=1)
