Hi there, I am reading values through MQTT from device that sends display values as series of ASCII codes, i.e 52 32 52.
Is there an easy way to convert this back to chars?
Hi there, I am reading values through MQTT from device that sends display values as series of ASCII codes, i.e 52 32 52.
Is there an easy way to convert this back to chars?
There’s discussion of a template here:
Don’t understand a word of it myself.
I solved it by brutal, but effective thing
{% set ascii = " !'#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~" %}
{{ ascii[value_json.CH1 - 32] + ascii[value_json.CH2 - 32] + ascii[value_json.CH3 - 32] }}
Method that doesn’t require hardcoding the ASCII table nor splitting into explicit separate characters.
{% set a = "72 101 108 108 111 32 119 111 114 108 100 33" %}
{% set b = a.split()|map('int')|list %}
{{ ("%c"*b|length)|format(*b) }}
Line 1 defines the input string, a series of space-separated numbers as you presented in the original post.
Line 2 breaks these up and turns them into a list of integers.
Line 3 then prints them out as a set of characters. That line expands to:
{{ "%c%c%c%c%c%c%c%c%c%c%c%c"|format(72,101,108,108,111,32,119,111,114,108,100,33) }}