Skip to main content

Data Type: WSTRING

According to the IEC 61131-3 standard, the data type WSTRING follows UCS-2 encoding.

UCS-2 encoding means that a character is encoded with exactly the fixed length 2 bytes. UCS-2 comprises the characters of the code points from U+0000 to U+D7FF and from U+E000 to U+FFFF. The string is terminated with 0.

Declaration:

<variable name> : WSTRING( <size> ) := " <text> "

The size <size> determines the string length. The initialization is done with the "<text>" string. The string is flagged as WSTRING with double quotes. The specification of the size and the initialization are optional.

When a variable of the data type WSTRING is reinitialized by resetting the application, the available byte pairs of the (old) string, which exist after the terminating null word (WORD) of the initial value, are not overwritten. This applies both to initialization with the initialization value and to initialization with the default initialization value 0.

Example 213. Example

Strings of the WSTRING data type require 2 bytes per character. In the case of the STRING data type, only 1 byte per character is reserved.

wsString : WSTRING := "This is a WSTRING";
sString : STRING := 'This is a STRING';

The string wsEmpty is the empty string. The variable wsMoney with the euro sign (€) has a size of 10. The string is truncated after 10 characters. The Chinese character (U+7BE6) is assigned to the variable wsHan.

PROGRAM PRG_Money
VAR
    wsEmpty : WSTRING := ""; // The empty string
    wsMoney : WSTRING(10) := "12345678 € 123"; // String will be truncated
    wsMoney_2 : WSTRING(10) := "12345678 €";
    wsHan : WSTRING(10) := "篦"; // Chinese character U+/BE6
END_VAR