# Convert string of char to decimal?

**URL:** https://discourse.nodered.org/t/convert-string-of-char-to-decimal/75596
**Category:** General
**Tags:** function-node
**Created:** [22 February 2023 16:36 UTC](https://discourse.nodered.org/t/convert-string-of-char-to-decimal/75596 "2023-02-22T16:36:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![SPS](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@SPS](https://discourse.nodered.org/u/SPS)
#### Post date: [22 February 2023 16:36 UTC](https://discourse.nodered.org/t/convert-string-of-char-to-decimal/75596/1 "2023-02-22T16:36:17Z")

</div>

Hey Experts,

I hope to find some helps here.  
At first I convert **INT to CHAR** --\> Easy, take a look:

```auto
// Split the string into array of individual values
var parts = msg.payload.split(" ");
// Remove the last blank part caused by the trailing comma
parts.pop();
// Parse each part of the string to a number
var bytes = parts.map(v => parseInt(v))
// Create a new Buffer from those bytes and convert back to String
msg.payload = Buffer.from(bytes).toString();
return msg;

```

It works... But now I like to do same in other direction... Hmmm  
Example:  
`WLC0270` --\> `87 76 67 48 50 55 48 ??`

I testing something but nothings works,  
at first I create an array for each character:

```auto
var parts = msg.payload.split("");

```

then me is missing a rule to convert `from(string).tobyte();`

So you see currently I´am not an expert yet.  
Hope anybody understand my issue.  
Thanks in advance.  
BG

---

<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: [22 February 2023 17:03 UTC](https://discourse.nodered.org/t/convert-string-of-char-to-decimal/75596/2 "2023-02-22T17:03:43Z")

</div>

Hi, welcome to the forum.

A couple of things firstly.

For better reading experience, please wrap code in a codeblock

`````  
`like this`  
`````

Next:

1. Where is this data coming from?
2. What does the incoming data look like (show us a small screen snippet of debug output)

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [22 February 2023 17:39 UTC](https://discourse.nodered.org/t/convert-string-of-char-to-decimal/75596/3 "2023-02-22T17:39:44Z")

</div>

I have moved this out of `creating nodes` on the basis its not referring to module development.

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [22 February 2023 21:14 UTC](https://discourse.nodered.org/t/convert-string-of-char-to-decimal/75596/4 "2023-02-22T21:14:27Z")

</div>

> [@SPS](#):
>
> `var parts = msg.payload.split("")`

Think that should be

```auto
var parts = msg.payload.split(" ")

```

---

<div class="post-metadata">

### Author: ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)
#### Post date: [23 February 2023 02:36 UTC](https://discourse.nodered.org/t/convert-string-of-char-to-decimal/75596/5 "2023-02-23T02:36:52Z")

</div>

How about this ?

```auto
let p = msg.payload
let decimal = []
for (let i = 0; i < p.length; i++) {
  decimal.push(p.charCodeAt(i))
}
msg.payload = decimal.join(" ")
return msg;

```

msg.payload : string[20]

"87 76 67 48 50 55 48"

---

<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: [24 April 2023 02:37 UTC](https://discourse.nodered.org/t/convert-string-of-char-to-decimal/75596/6 "2023-04-24T02:37:22Z")

</div>

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