STRING
The data type STRING is interpreted as Latin-1 or UTF-8.
Common properties and differences are described below.
A variable of data type STRING can accept any string. The size of the storage space reservation in the declaration refers to the number of bytes and is enclosed in round or square brackets. If no size is specified, 80 bytes are reserved by default. There is also a byte for the final zero.
As a rule, TwinCAT does not limit the string length, but the string functions only process lengths between 1 and 255. If a variable is initialized with a literal that is too long for the data type of the variable, the literal is truncated accordingly from the right.
If a variable of the data type STRING is reinitialized by a reset of the application, the content of the (old) string after the final zero character of the initial value is not overwritten. This applies both to initialization with the initialization value and to initialization with the default initialization value 0.
Latin-1
Latin-1 encoding comprises a restricted character set, which also includes all ASCII characters.
With Latin-1 encoding, the storage space required for a STRING variable is always 1 byte per character plus one additional byte. This results in 81 bytes in the case of a standard STRING(80) declaration, for example. |
Sample: String declaration with storage space reservation 46+1 bytes for 46 characters.
sVar : STRING(46) := 'This is a string with memory for 46 characters.';
UTF-8
The UTF-8 encoding includes the largest possible character set.
The UTF-8 encoding of Unicode characters is identical to Latin-1 encoding in the case of ASCII characters. All characters beyond this differ in the dump.
In this case, add the pragma {attribute 'TcEncoding':='UTF-8'} to the declaration of a variable. As a result, the content of the variables is also displayed in UTF-8 encoding in the monitoring. Further details can be found in the description of the 'TcEncoding' attribute.
Only use string processing functions that are specifically released for UTF-8 encoded strings. See UTF8Len() and others.
With UTF-8 encoding, a character can be up to 4 bytes long. However, the maximum length of the string specified in the declaration always indicates the number of bytes that are reserved for the STRING. This means that there is no longer a 1:1 relationship between the number of characters and the amount of storage space required. |
Sample: String declaration with storage space reservation 46+1 bytes for up to 46 characters. The number of possible characters depends on the characters themselves.
{attribute 'TcEncoding':='UTF-8'}
sVar : STRING(46) := 'This is a string with memory for up to 46 characters.';
See also: