Boolean conversion

Boolean conversion 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!

Boolean conversion 2:

Loss of information possible

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

Boolean conversion 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.

<type>_TO_BOOL

The operators are used to convert from another data type to the BOOL data type.

You can specify a variable or a literal as the operand.

Syntax:

<Datentyp>_TO_BOOL (<Variablenname> | <Literal>)

The result is TRUE if the operand is not equal to 0. The result is FALSE if the operand is 0.
With the STRING data type, the result is TRUE if the operand value is 'TRUE' or 'true'. Otherwise, FALSE is returned even if 'True'.

Examples:

ST code

Result

bVar := BYTE_TO_BOOL(2#11010101);

TRUE

bVar := INT_TO_BOOL(0);

FALSE

bVar := TIME_TO_BOOL(T#5ms);

TRUE

bVar := STRING_TO_BOOL('TRUE');

TRUE

bVar := STRING_TO_BOOL('true');

TRUE

bVar := STRING_TO_BOOL('True');

FALSE

BOOL_TO_<type>

The operators are used to convert the BOOL data type to another data type.

You can specify a variable or a literal as the operand.

Syntax:

BOOL_TO_<Datentyp> (<Variablenname> | <Literal>)

With numerical data types the result is 1 if the operand is TRUE and 0 if the operand is FALSE. With the data type STRING the result is 'TRUE' or 'FALSE'.

Examples:

ST code

Result

nVar := BOOL_TO_INT(TRUE);

1

sVar := BOOL_TO_STRING(TRUE);

'TRUE'

tVar := BOOL_TO_TIME(TRUE);

T#1ms

tVar := BOOL_TO_TOD(TRUE);

TOD#00:00:00.001

dVar := BOOL_TO_DATE(FALSE);

D#1970

dtVar := BOOL_TO_DT(TRUE);

DT#1970-01-01-00:00:01

FBD code

Result

Boolean conversion 4:

1

Boolean conversion 5:

'TRUE'

Boolean conversion 6:

T#1ms

Boolean conversion 7:

TOD#00:00:00.001

Boolean conversion 8:

D#1970-01-01

See also: