STRING type variables

STRING also has two subtypes, just like CHAR. The old, “old-school” STRING, which describes the text with ASCII characters, and WSTRING, which uses WCHAR characters with two bytes per character. Both types are suitable for storing text, which can be extremely useful for communication, especially in HMI connections.

For both types, the first two positions show the maximum length of the given STRING and the current length it has been filled with. One position equals one byte for STRING, and one word for WSTRING.

STRING type variables 1st examples

In the example above, taken from the PLC status, I entered the phrase “lama!” into an 8-byte STRING variable. The first two bytes contain the maximum length of the STRING (8) and the current length (5), followed by the phrase as our message.

If I change the display format to hexadecimal for the characters, I see the ASCII code for each letter.

STRING type variables 2nd examples

That is, the letter “l” is ASCII 16#6C, and “a” is ASCII 16#61, …

For WSTRING, this assignment appears like this:

STRING type variables 3rd examples

The “$00l” content type is due to the nature of UNICODE, as “simple” characters do not fill the entire UCS-2 space. It is clear that while we counted the positions per byte above, in this case each position occupies a word. The first two words here also contain the maximum length of the STRING (8) and the current length (5).

The same definition is given in hexadecimal form as follows:

STRING type variables fourth examples

If we fully fill in the UCS-2 word field, we can see what the “non-simple characters” look like. In the first step, I entered longer codes in the word variables per character (1), and from this the “example” WSTRING (2) was displayed:

STRING type variables fifth examples

To sum it all up:

TypeLengthCharacter encodingLength (characters)Example
STRING 2byte + textCHAR, ASCII0 .. 254 byte / character'lamaPLC', STRING#'lamaPLC'
WSTRING2 word + textWCHAR, UNICODE0 .. 16382 word / characterWSTRING#lamaPLC