# Try to convert msg.payload string of number into ascii

**URL:** <https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193>\
**Category:** General\
**Tags:** javascript\
**Created:** [14 February 2023 18:33 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193 "2023-02-14T18:33:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhilLu](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@PhilLu](https://discourse.nodered.org/u/PhilLu)\
**Post date:** [14 February 2023 18:33 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/1 "2023-02-14T18:33:39Z")

</div>

Hello,  
I have to translate a string of delimited numbers into ascii string.  
This is working fine:  
`String.fromCharCode(65,66,67); // returns 'ABC'`  
...  
I have a msg.payload containing by example: 45,42,45,42,45,42,45,42,22,42,22,42,6,42,6,28,28

I need to translate that string into ascii like this: X0017EXXU5 (from the above example payload)  
I try:  
`msg.payload = String.fromCharCode(msg.payload); return msg;`  
...in a function node

but I receive a strange aswer:  
""  
Strange ???

If someone have an idea???  
Many thanks,  
PhilLu

---

<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 February 2023 20:10 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/2 "2023-02-14T20:10:53Z")

</div>

Welcome to the forum @PhilLu

So we can see exactly what you have, can you feed the incoming data into a debug node and show us a screenshot of what you see please.

---

<div class="post-metadata">

**Author:** ![PhilLu](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@PhilLu](https://discourse.nodered.org/u/PhilLu)\
**Post date:** [15 February 2023 11:45 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/3 "2023-02-15T11:45:23Z")

</div>

Thank you for your answer 😉  
The first screenshot is the initial payload to use.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/3/f3abefc512dec4a1b966e3dfafc3b686df3264f2.png)

This is the code

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

```

The last is the debug.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a277602d4c0e0b960c3123c9e49cda5f7e5f36dd.png)

Many thanks for your assistance 🙂  
Phil.

---

<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:** [15 February 2023 16:18 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/4 "2023-02-15T16:18:03Z")

</div>

The string` 45,42,45,42,45,42,45,42,22,42,22,42,6,42,6,28,28` represents `

```auto
-*-*-*- ****

```

not `X0017EXXU5` which would be `'88,48,48,49,55,69,88,88,85,53'`,

However, a quick and dirty method

```auto
let input = ' 45,42,45,42,45,42,45,42,22,42,22,42,6,42,6,28,28'

let numbers = input.split(',')
let output = ''

for (const element of numbers) {
    output = output + String.fromCharCode(parseInt(element, 10))
}

msg.payload = output

return msg

```

---

<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:** [15 February 2023 16:25 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/5 "2023-02-15T16:25:40Z")

</div>

Maybe these are hexadecimal values?

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

One thing is certain, they do not equal what the OP states

---

<div class="post-metadata">

**Author:** ![PhilLu](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@PhilLu](https://discourse.nodered.org/u/PhilLu)\
**Post date:** [17 February 2023 10:31 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/6 "2023-02-17T10:31:51Z")

</div>

Hello, many thanks for your answer 😉  
Strange about the collected string.  
I use an usb barcode scanner on my RPI.  
When I am on a simple text and I scan the barcode, I receive the correct text:  
X0017EXXU5  
Then, to collect that string in Node-red, I use the node Pi Keyboard.  
When I scan the barcode, I receive several separated numbers.  
To have a comma separated string I use JOIN with comma separator, giving:

```auto
42,45,42,45,42,11,42,11,42,11,42,11,42,2,42,2,42,8,42,8,42,18,42,18,42,45,42,45,42,45,42,45,42,22,42,22,42,6,42,6,28,28

```

Is it the wrong method to collect simple string from barcode?  
Manythnks for your help 😉  
Phil.

---

<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:** [17 February 2023 23:18 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/7 "2023-02-17T23:18:25Z")

</div>

Probably need to check what the scanner is actually sending (from the manual) because it certainly isn't a keystroke copy of the text.

---

<div class="post-metadata">

**Author:** ![PhilLu](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@PhilLu](https://discourse.nodered.org/u/PhilLu)\
**Post date:** [18 February 2023 09:53 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/8 "2023-02-18T09:53:14Z")

</div>

Hello,  
Many thanks for your answer 😉  
The strange think is when I use the scanner in a simple editor on the same system without any modification, it works perfectly:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2ebf2cdf91516de3e04b9a51f625ce688e785821.png)  
How is it possible to get that kind of message i-as payload into Node-Red?  
Thanks for your suggestions 👌  
Phil.

---

<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 2023 10:19 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/9 "2023-02-18T10:19:48Z")

</div>

Often, barcode scanners have 2 modes - keyboard mode and data mode (serial / ethernet for example).

If your device supports a different mode, you can bypass the whole keyboard shenanigans and use a serial node to read the raw data that it sends. This is often faster and more reliable than a keyboard mode.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [18 February 2023 11:28 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/10 "2023-02-18T11:28:23Z")

</div>

The pi keyboard node returns key scan codes as it grabs the keys at the hardware level. So it is not ascii codes

---

<div class="post-metadata">

**Author:** ![PhilLu](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@PhilLu](https://discourse.nodered.org/u/PhilLu)\
**Post date:** [18 February 2023 12:28 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/11 "2023-02-18T12:28:23Z")

</div>

Many thanks for all your answers 😉  
Now I use Node-red Dashbord input text node and perfectly get barcode when scanned 👌  
Is it possible to hide dashbord (work without screen) and focus that input text at boot?  
Also, is it possible to clear that field after 1sec by example?

Have a nice Sunday,  
Many thanks, 🙏  
Phil.

---

<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 2023 12:42 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/12 "2023-02-18T12:42:35Z")

</div>

> [@PhilLu](#):
>
> Is it possible to hide dashbord (work without screen)

Not sure since it needs to "type" into a focused input, in keyboard mode.

However, if you can switch to data (serial? Ethernet?) You won't need dashboard at all.

---

<div class="post-metadata">

**Author:** ![PhilLu](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@PhilLu](https://discourse.nodered.org/u/PhilLu)\
**Post date:** [18 February 2023 12:46 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/13 "2023-02-18T12:46:40Z")

</div>

Impossible to install node-red-contrib-usbhid in palette 😶  
Don't now how to bypass that or use alternate solution...

---

<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 2023 12:51 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/14 "2023-02-18T12:51:29Z")

</div>

> [@PhilLu](#):
>
> Impossible to install node-red-contrib-usbhid in palette

[Search the forum](https://discourse.nodered.org/search?context=topic&context_id=75193&q=node-red-contrib-usbhid&skip_context=true)

> [@PhilLu](#):
>
> Don't now how to bypass that or use alternate solution

Have you read the manual?

---

<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:** [19 April 2023 12:51 UTC](https://discourse.nodered.org/t/try-to-convert-msg-payload-string-of-number-into-ascii/75193/15 "2023-04-19T12:51:43Z")

</div>

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