String conversion

String conversion 1:

Undefined result if the value range is exceeded

If the input value of a type conversion operator is outside the value range of the output data type, the result of the operation is not defined and depends on the platform. This is the case, for example, when a negative operand value is converted from LREAL to the target data type UINT. An exception error is also possible!

String conversion 2:

Loss of information possible

If a larger data type is converted to a smaller data type, information may be lost.

String conversion 3:

String manipulation when converting to STRING or WSTRING

With a type conversion to STRING or WSTRING, the typed value is stored as a left aligned string and truncated if it is overlong. Therefore, declare the return variables for the type conversion operators <type>_TO_STRING and <type>_TO_WSTRING long enough to accommodate the string without manipulation.

<type>_TO_STRING

The operators are used to convert from another data type to the STRING or WSTRING data type.

You can specify a variable or a literal as the operand.

Syntax:

<Datentyp>_TO_STRING (<Variablenname> | <Literal>)
<Datentyp>_TO_WSTRING (<Variablenname> | <Literal>)

STRING_TO_<type>

The operators are used to convert the STRING or WSTRING data type to another data type.

You can specify a variable or a literal as the operand.

Syntax:

STRING_TO_<Datentyp> (<Variablenname> | <Literal>)
WTRING_TO_<Datentyp> (<Variablenname> | <Literal>)

You must specify the operand of type STRING in accordance with the IEC 61131-3 standard. The value must be equivalent to a valid constant (literal) of the target data type. This applies to exponential values, infinite values, prefixes, grouping characters ("_") and commas. Additional characters are allowed behind the digits of a number, e.g. 23xy. Additional characters before a number are not allowed.

The operand must represent a valid value of the target data type.

Converting STRING/WSTRING to BOOL

Operators:

STRING_TO_BOOL (<Variablenname> | <Literal>)
WSTRING_TO_BOOL (<Variablenname> | <Literal>)

A TRUE is only returned if the operand value is 'TRUE' or 'true'. On the other hand, 'True' returns a FALSE.

Examples:

ST code

Result

bVar := STRING_TO_BOOL('TRUE');

TRUE

bVar := STRING_TO_BOOL('true');

TRUE

bVar := STRING_TO_BOOL('True');

FALSE

nVar := STRING_TO_WORD('abc34');

0

nVar := STRING_TO_WORD('34abc');

34

tVar := STRING_TO_TIME('T#127ms');

T#127ms

fVar := STRING_TO_REAL('1.234');

1,234

nVar := STRING_TO_BYTE('500');

244

TO_STRING/TO_WSTRING for enumeration variables

If you wish to query the textual identifier of an enumeration component, for example in order to process it further in a text output, add the Attribute 'to_string' above the declaration of the enumeration. In the implementation part you can then apply the conversion function TO_STRING or TO_WSTRING to an enumeration variable or to the components of the enumeration. The name of the enumeration component will then be returned.

String conversion 4:

Attribute 'to_string'

Available from TC3.1 Build 4024

The conversion functions TO_STRING/TO_WSTRING can also be applied to enumerations that are not declared with the attribute 'to_string'. In this case the numerical value of the enumeration component will be returned.

Sample:

Enumeration E_Sample

{attribute 'qualified_only'}
{attribute 'strict'}
{attribute 'to_string'}
TYPE E_Sample :
(
    eInit := 0,
    eStart,
    eStop
);
END_TYPE

Program MAIN

PROGRAM MAIN
VAR
    eSample         : E_Sample;
    nCurrentValue   : INT;
    sCurrentValue   : STRING;
    wsCurrentValue  : WSTRING;
 
    sComponent      : STRING;
    wsComponent     : WSTRING;
END_VAR
nCurrentValue  := eSample; 
sCurrentValue  := TO_STRING(eSample);
wsCurrentValue := TO_WSTRING(eSample);

sComponent     := TO_STRING(E_Sample.eStart);
wsComponent    := TO_WSTRING(E_Sample.eStop);

 

Result of the assignments/conversion functions:

 

Result if the enumeration were not to be declared with the attribute 'to_string':

See also: