Function Node to Convert Number to ASCII Format

I am trying to make a function node that will convert a number like "9" to its ASCII equivalent. I saw something on GitHub but it looks like the library doesn't exist anymore. I know I need to use the "charCodeAt" prototype but I am not sure how to format it in the function node correctly.

You can use charCodeAt(0), but keep in mind that it requires a string to work, so numbers need to be converted as string.

[{"id":"7ea6aae1.ed4c24","type":"inject","z":"681f8bde.8887bc","name":"","topic":"","payload":"9","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":302,"y":504,"wires":[["f08dfe53.514ae8"]]},{"id":"fed7baa6.c5bba","type":"debug","z":"681f8bde.8887bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":602,"y":504,"wires":[]},{"id":"f08dfe53.514ae8","type":"function","z":"681f8bde.8887bc","name":"","func":"m = msg.payload\no = m.charCodeAt(0);\n\nreturn {payload:o}","outputs":1,"noerr":0,"x":442,"y":504,"wires":[["fed7baa6.c5bba"]]}]

This example wil only work for a single character.

Do you have any ideas on how to get it to handle multiple characters?

Sure, it is the same right, just needs a loop and an array.

[{"id":"7ea6aae1.ed4c24","type":"inject","z":"681f8bde.8887bc","name":"","topic":"","payload":"helllo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":302,"y":504,"wires":[["f08dfe53.514ae8"]]},{"id":"fed7baa6.c5bba","type":"debug","z":"681f8bde.8887bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":602,"y":504,"wires":[]},{"id":"f08dfe53.514ae8","type":"function","z":"681f8bde.8887bc","name":"","func":"ascii = [];\n\nfor (var i = 0; i < msg.payload.length; i ++)\n  ascii.push(msg.payload[i].charCodeAt(0));\n  \n\nreturn {payload:ascii}","outputs":1,"noerr":0,"x":442,"y":504,"wires":[["fed7baa6.c5bba"]]}]
1 Like

Thank you, that worked perfectly.