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);General conversion — <FROM>_TO_<TO>
Section titled “General conversion — <FROM>_TO_<TO>”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.
Source / target type set
Section titled “Source / target type set”The same set is used for both the <FROM> and <TO> side (a type is never
converted to itself):
| Group | Types |
|---|---|
| Boolean | BOOL |
| Bit string | BYTE, WORD, DWORD, LWORD |
| Signed integer | SINT, INT, DINT, LINT |
| Unsigned integer | USINT, UINT, UDINT, ULINT |
| Real | REAL, LREAL |
| Character | CHAR, WCHAR |
| String | STRING, WSTRING |
| Duration | TIME, LTIME |
| Date | DATE, LDATE |
| Time of day | TOD, LTOD |
| Date and time | DT, LDT |
Real truncation — <REAL>_TRUNC_<TO>
Section titled “Real truncation — <REAL>_TRUNC_<TO>”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 4BCD conversion — _BCD_TO_ and _TO_BCD_
Section titled “BCD conversion — _BCD_TO_ and _TO_BCD_”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 thumbwheeldisplay := INT_TO_BCD_WORD(value); // re-encode for a BCD displayUse IS_VALID_BCD
to check that a bit string holds a valid BCD value before decoding it.