# Parse 8 bit value from string

**URL:** <https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224>\
**Category:** General\
**Created:** [15 December 2021 09:08 UTC](https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224 "2021-12-15T09:08:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [15 December 2021 09:08 UTC](https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224/1 "2021-12-15T09:08:52Z")

</div>

I am receiving this string from a Pic micro using an RS484 to serial (USB) converter.

FE5A6144141A140C1413142324222000�

The last char, showing as a replacement char of something that cannot be displayed, it is a 8bit value.  
It represent the checksum.  
I am extracting that char from the string using:

```auto
let cs = m.substr(33,1);

```

This gives me this: �  
I have tried to convert it using:

```auto
cs=String.fromCharCode(cs);

```

This gives me an empty string " "  
and

```auto
cs=parseInt(cs, 8)

```

This gives me a NaN

Whatever I tried is not returning me the value. I have successfully decoded the rest of the string but not that last part!  
Here is the decoding function with the string I receive copied into an injection node.

```auto
[{"id":"5780c7918e4f68a9","type":"function","z":"b114e20b20dbeb01","name":"","func":"let m = msg.payload\n\nlet mAddr = m.substr(1,2)\nlet sAdrr = m.substr(3,2);\nlet comm = m.substr(5,2);\nlet vc1 = m.substr(9,2) + m.substr(7,2);\nlet vc2 = m.substr(13,2)+ m.substr(11,2);\nlet vc3 = m.substr(17,2)+ m.substr(15,2);\nlet vc4 = m.substr(21,2);+ m.substr(19,2)\nlet t1 = m.substr(23,2);\nlet t2 = m.substr(25,2);\nlet t3 = m.substr(27,2);\nlet t4 = m.substr(29,2);\nlet flag = m.substr(31,2);\n//let cs = m.substr(33,1);\nlet cs = m.substr(33,1);\n\nvc1=parseInt(vc1, 16)/1000;\nvc2=parseInt(vc2, 16)/1000;\nvc3=parseInt(vc3, 16)/1000;\nvc4=parseInt(vc4, 16)/1000;\nt1=parseInt(t1, 16);\nt2=parseInt(t2, 16);\nt3=parseInt(t3, 16);\nt4=parseInt(t4, 16);\n//cs=String.fromCharCode(cs);\n//cs=parseInt(cs, 8)\n\n\n//formatting\nreturn {payload:{MasterAddress:mAddr,\nSlaveAddress:sAdrr,\nCommand:comm,\nVc1:vc1,\nVc2:vc2,\nVc3:vc3,\nVc4:vc3,\nT1:t1,\nT2:t2,\nT3:t3,\nT4:t4,\nFlag:flag,\nCS:cs\n}}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":440,"wires":[["23556ff73afb7655"]]},{"id":"23556ff73afb7655","type":"debug","z":"b114e20b20dbeb01","name":"Decode","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":580,"y":440,"wires":[]},{"id":"b521d8c461b7e66d","type":"inject","z":"b114e20b20dbeb01","name":"Dummy Value","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\u0002FE5A6144141A140C1413142324222000�","payloadType":"str","x":300,"y":520,"wires":[["5780c7918e4f68a9"]]}]

```

How can I get and parse that 8bit value to an integer?  
Thanks for helping.

---

<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 December 2021 09:25 UTC](https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224/2 "2021-12-15T09:25:57Z")

</div>

What do you get if you use `Buffer.from()`

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [15 December 2021 09:32 UTC](https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224/3 "2021-12-15T09:32:16Z")

</div>

Hi Colin,  
I am learning on the go... I am not a coder.  
How do I use Buffer.from() in this context?

---

<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 December 2021 09:54 UTC](https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224/4 "2021-12-15T09:54:00Z")

</div>

In a function node, assuming the string is in msg.payload

```auto
msg.payload = Buffer.from(msg.payload)
return msg

```

Then feed it into a debug node.  
Do you have control of the message? If so then change that code to send the data in, for example, JSON.

**[Edit]** I meant the data coming from the micro.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [15 December 2021 11:07 UTC](https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224/5 "2021-12-15T11:07:55Z")

</div>

Thank you Colin,  
that's quite interesting. I get an object with 4 arrays containing 36 values.  
I will take it from there and rewrite the decoding function or just pickup the troubling value from the buffer arrays.

 ![Schermata 2021-12-15 alle 13.04.44](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/290b64929fee87db3b3615e00fba7dc5acd93b4b.png)

Thanks a million.

---

<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 December 2021 11:39 UTC](https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224/6 "2021-12-15T11:39:01Z")

</div>

That's 1 buffer, with 37 values (it is showing them in blocks of 10 so you can access them more easily in the debug window).  
You appear to have 33 printable characters, ending in two zeros (0x30) then there are four additional bytes. So what do you want out of those?

Are you in control of the pic code? If so then change it to make the data easier to extract.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [15 December 2021 13:07 UTC](https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224/7 "2021-12-15T13:07:57Z")

</div>

I am not in control of the Pic code. There are 4 voltage measurements and 4 temperatures which I have extracted using substr(). I was battling with the last value which is a checksum to verify if the returned string is valid. But now I can get that value from the buffer and complete the verification.  
Thank you again for the directions.

---

<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:** [29 December 2021 13:08 UTC](https://discourse.nodered.org/t/parse-8-bit-value-from-string/55224/8 "2021-12-29T13:08:18Z")

</div>

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