How to convert BYTE to ASCII characters?

What’s the best way of converting a BYTE into ASCII characters? I have a BYTE value of 0100 1110 or 78, and want to convert it to its ASCII character equivalent of “N”. Is there a library that can do this?

The bits 0100 1110 are the same regardless of the data type.
The answer to this question may depend on the language used and the desired function - how is “N” used?

Here’s an example in Structured Text. Character data types do not exist in IEC 61131-3 so a STRING of length 1 (size 2) is used.

(The size of MyString needs to match the size of MyData).

Additionally, the Automation Studio Watch can display the same data in different representations.

Here’s a STRING representation.
image

Here’s a binary representation.
image

4 Likes

That worked perfectly, thanks! Yes my intention was for the “N” to be a string. What is the purpose of the null terminator though? I’ve tested without having an array of the bytes, and it seems to work fine.

The computer reads strings starting at their memory address until the null terminator. You may have gotten lucky that the byte in memory follow the 'N' was value 0.

If I intentionally force the byte in memory after 'N' to non-zero (this is only for example, I do not recommend this in practice), the computer reads more bytes than just 'N'.

The same memory laws from C strings apply to IEC strings. Here’s a link about C strings (there’s more online tutorials for C than IEC).

2 Likes

Hi Jack,

If Tyler’s solution worked for you, please mark the reply that contains the solution to your question so that others with the same question can find the answer more easily.

Thank you!