Type conversion operators

With type conversions, a distinction is made between implicit and explicit type conversions.

Implicit type conversions

Conversions from a "smaller" type to a "larger" type, such as from BYTE to INT or from WORD to DINT, can be carried out explicitly - but they are also possible implicitly without calling a conversion operator.

Explicit type conversions

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.

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 spellings for the data types TOD and DT.

TIME_OF_DAY and DATE_AND_TIME are not mapped as type conversion commands. Use the data types TOD and DT instead.

Type conversion operators 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!

Type conversion operators 2:

Loss of information possible

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

Type conversion operators 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.

See also: