Type conversion operators

You can explicitly call type conversion operators. For typed conversions from one elementary type to another elementary type and also for overloads, the type conversion operators described below are available. Conversions from a "smaller" type to a "larger" type, such as from BYTE to INT or from WORD to DINT, are also possible implicitly.

Typed conversion: <elementary data type>_TO_<another elementary data type>

Overloaded conversion: TO_<elementary data type>

Elementary data types:

<elementary data type> =
__UXINT | __XINT | __XWORD | BIT | BOOL | BYTE | DATE | DINT | DT | DWORD | INT | LDATE | LDT | LINT | LREAL | LTIME | LTOD | LWORD | REAL | SINT | TIME | TOD | UDINT | UINT | ULINT | USINT | WORD

The keywords TIME_OF_DAY and DATE_AND_TIME are alternative notations for the data types TOD and DT. TIME_OF_DAY and DATE_AND_TIME are not mapped as a type conversion command.

Type conversion operators 1:

For a type conversion operator, if the operand value is outside the value range of the target data type, the result output is undefined. This is the case, for example, when a negative operand value is converted from LREAL to the target data type UINT.

Information may be lost during type conversion from larger to smaller types.

Type conversion operators 2:

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.

See also: