0

Is there any function block which allows conversion of string to ASCII code and vice versa in TwinCAT?

I found this function f_ToCHR, but it only converts one character at a time and for converting the whole string, I would need to put it in a for loop, which would not be optimal.

Is there any function that could do the whole string conversion, not character by character?

Roald
  • 2,459
  • 16
  • 43
Piper
  • 149
  • 11

1 Answers1

5

You could create a UNION with a string and a byte array. This will put them in the same memory space, and since a string is simply a series of ASCII bytes, the individual character values will end up in each array element.

TYPE testUnion
UNION
  stTest : STRING;
  arTest : ARRAY[0..79] OF BYTE;
END_UNION
END_TYPE

enter image description here

kolyur
  • 457
  • 2
  • 13