Data type conversion

When you are creating your application, there will be situations when you have to adapt the supported data types for the use in expressions or assignments. logi.CAD 3 provides the following type conversions for this adaption:

Explicit conversion of data type

Enter the call of one of the convert functions when creating your application in order to convert a data type into the required data type. Observe the conversion rules.

Possible results of an explicit conversion

  • loss of accuracy

  • mismatch in the range

Implicit conversion of data type

Due to the implicit conversion of logi.CAD 3 you do not have to enter additional code in your application, if you want to convert the following data types:

images/download/attachments/409863261/DatatypesConvertingFigure_EN-version-1-modificationdate-1531296823361-api-v2.png

Note on how to interpret the figure: The arrows present the possible conversions paths. Therefore: A data type can be converted to the data type to which the arrow points. If this data type can be converted to data types again, the data type in front can be converted to the following data types as well.

  • Example 1: BOOL can be converted to BYTE. As BYTE can be converted to WORD, BOOL can be converted to WORD as well. As WORD can be converted to DWORD, BYTE and BOOL can be converted to DWORD as well. And so on, along the direction of arrow.

  • Example 2: SINT can be converted to INT. As INT can be converted to DINT and REAL, SINT can be converted to DINT and REAL. And so on, along the direction of arrow.

Results of an implicit conversion: Value and accuracy of the original data type is kept. However, observe the conversion rules.

Conversion rules

  • When converting a REAL/LREAL value into an ANY_INT value, it is rounded up or down to the next →integer. Half portions (such as 0.5) are rounded to the next even integer. Example: The REAL value 1.5 and the REAL value 2.5 are both converted to ANY_INT value 2.

  • When explicitly converting an ANY_SIGNED value (a signed value) into an ANY_UNSIGNED value (an unsigned value) and vice versa, the bit pattern of the value is interpreted in the specific data format.

    Hence:

    Original value

    Result (converted value)

    -1 (as signed value)

    FFhex or FFFFhex or FFFFFFFFhex
    (as unsigned value, depending on the data type)

    128 (=80hex) as USINT

    -128 (=80hex) as SINT

    129 (=81hex) as USINT

    -127 (=81hex) as SINT

    255 (=FFhex) as USINT

    -1 (=FFhex) as SINT

  • If converting data types to the TIME data type and vice versa, the conversion is always based on seconds.