# Convert decimal to char

**URL:** https://discourse.nodered.org/t/convert-decimal-to-char/23496
**Category:** General
**Created:** [23 March 2020 10:41 UTC](https://discourse.nodered.org/t/convert-decimal-to-char/23496 "2020-03-23T10:41:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tarahi](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@tarahi](https://discourse.nodered.org/u/tarahi)
#### Post date: [23 March 2020 10:41 UTC](https://discourse.nodered.org/t/convert-decimal-to-char/23496/1 "2020-03-23T10:41:03Z")

</div>

Hi my friends,  
In part of my flow, I faced an output in decimal format like this :

 ![python5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/59d16cf98f70e6e0a1d52b14ab0c08a8a2bcc25a.jpeg)

```auto
[{"id":"7c5d1a66.b99a74","type":"tab","label":"Flow 9","disabled":false,"info":""},{"id":"74bc4c7d.bac1e4","type":"debug","z":"7c5d1a66.b99a74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":450,"y":80,"wires":[]},{"id":"4e14428e.9c0dac","type":"inject","z":"7c5d1a66.b99a74","name":"","topic":"","payload":"50, 54, 46, 48, 48,","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":120,"wires":[["74bc4c7d.bac1e4"]]}]

```

As you see below :  
[https://upload.wikimedia.org/wikipedia/commons/d/dd/ASCII-Table.svg](https://upload.wikimedia.org/wikipedia/commons/d/dd/ASCII-Table.svg)  
these are decimal code that will be "26.00" in char format  
How can I convert any decimall input to char format ?  
Thanks

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 March 2020 11:04 UTC](https://discourse.nodered.org/t/convert-decimal-to-char/23496/2 "2020-03-23T11:04:16Z")

</div>

For the sake of this example you want to convert `msg.payload`

Try `String(msg.payload)`

---

<div class="post-metadata">

### Author: ![tarahi](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@tarahi](https://discourse.nodered.org/u/tarahi)
#### Post date: [23 March 2020 11:11 UTC](https://discourse.nodered.org/t/convert-decimal-to-char/23496/3 "2020-03-23T11:11:00Z")

</div>

I used a function node with this code, but noting changed !

```auto
msg.payload=String(msg.payload)
return msg;

```

```auto
msg.payload : string[19]
"50, 54, 46, 48, 48,"

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [23 March 2020 11:12 UTC](https://discourse.nodered.org/t/convert-decimal-to-char/23496/4 "2020-03-23T11:12:49Z")

</div>

Hi @tarahi

Given an input of the string `"50, 54, 46, 48, 48,"`, you need to first convert that to an array of bytes.

The format isn't ideal as it isn't in a standard like JSON. If you are able to change whatever is producing that string, that would make your life easier.

But if we take the format you have shared as a given, then...

```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;

```

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 March 2020 11:13 UTC](https://discourse.nodered.org/t/convert-decimal-to-char/23496/5 "2020-03-23T11:13:40Z")

</div>

I see @knolleary is replying.

I think I shall let him answer the question better than I can.

But I think there is a problem with what you are typing.  
`String[19]` (note the capitol S) will make `19` a string.  
But I think there is a problem with the "50, 54, 46, 48, 48".... I think that is an array.

---

<div class="post-metadata">

### Author: ![tarahi](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@tarahi](https://discourse.nodered.org/u/tarahi)
#### Post date: [23 March 2020 11:18 UTC](https://discourse.nodered.org/t/convert-decimal-to-char/23496/6 "2020-03-23T11:18:36Z")

</div>

> [@knolleary](#):
>
> // 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;

Exactly what I wanted ! ❤ ❤ ❤  
Thanks a lot ... 😘

---

<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: [6 April 2020 11:18 UTC](https://discourse.nodered.org/t/convert-decimal-to-char/23496/7 "2020-04-06T11:18:38Z")

</div>

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