Skip to content

Type conversion

logiccloud does not hand-write one conversion function per type pair. Instead it generates a conversion function for every valid combination of source and target type, following a small set of naming rules. Rather than listing the hundreds of resulting names, this page documents the rules and the type set they range over — once you know the rule, you know the function name.

Every conversion function takes a single input parameter named IN of the source type and returns the target type.

r := INT_TO_REAL(counter);
b := REAL_TO_BYTE(level);

The general rule produces a function for every ordered pair of distinct types drawn from the conversion type set below:

<FROM>_TO_<TO>(IN : <FROM>) : <TO>

Examples: INT_TO_REAL, REAL_TO_DINT, WORD_TO_INT, BOOL_TO_BYTE, STRING_TO_INT, TIME_TO_LTIME, DATE_TO_DT.

The same set is used for both the <FROM> and <TO> side (a type is never converted to itself):

GroupTypes
BooleanBOOL
Bit stringBYTE, WORD, DWORD, LWORD
Signed integerSINT, INT, DINT, LINT
Unsigned integerUSINT, UINT, UDINT, ULINT
RealREAL, LREAL
CharacterCHAR, WCHAR
StringSTRING, WSTRING
DurationTIME, LTIME
DateDATE, LDATE
Time of dayTOD, LTOD
Date and timeDT, LDT

In addition to the rounding behavior of the general rule, the real types get a dedicated truncating conversion to every target type. Truncation discards the fractional part rather than rounding it.

REAL_TRUNC_<TO>(IN : REAL) : <TO>
LREAL_TRUNC_<TO>(IN : LREAL) : <TO>

The <TO> side ranges over the full conversion type set above. Examples: REAL_TRUNC_INT, REAL_TRUNC_DINT, LREAL_TRUNC_LINT.

whole := REAL_TRUNC_INT(3.9); // 3, not 4

BCD (binary-coded decimal) conversions are generated only between the integer, bit-string and string groups — i.e. <FROM> and <TO> are each one of:

  • Signed integers: SINT, INT, DINT, LINT
  • Unsigned integers: USINT, UINT, UDINT, ULINT
  • Bit strings: BYTE, WORD, DWORD, LWORD
  • Strings: STRING, WSTRING

Two directions are generated for each ordered pair:

<FROM>_BCD_TO_<TO>(IN : <FROM>) : <TO> // interpret IN as BCD, produce binary <TO>
<FROM>_TO_BCD_<TO>(IN : <FROM>) : <TO> // encode IN as BCD into <TO>

Examples: BYTE_BCD_TO_INT, INT_TO_BCD_WORD, WORD_BCD_TO_USINT.

value := BYTE_BCD_TO_INT(thumbwheel); // decode a BCD thumbwheel
display := INT_TO_BCD_WORD(value); // re-encode for a BCD display

Use IS_VALID_BCD to check that a bit string holds a valid BCD value before decoding it.