General Information
For general hints to be considered during type conversion, refer to the chapter Type Conversion Functions.
Definition
IEC operator for conversions from the variable type STRING to a different type.
Syntax
STRING_TO_<data type>
Specifying Values
Specify the operand of type STRING matching the IEC61131-3 standard. The value must correspond to a valid constant (literal) of the target type.
Convertible strings can contain:
-
Number with type prefix (example: '16#FFFFFFFF'
)
-
Number with grouping characters (example: '2#1111_1111'
)
NOTE: Use the underscore, as the international weight and measure grouping character (thin space) is not permitted.
-
Floating-point number, also in exponential notation (example: '9.876'
or '1.2E-34'
)
NOTE: Floating-point numbers are not convertible. The comma is treated as a following character and therefore truncated.
-
Time, time of day, and date specification with prefix and size (example: 'T#2h'
, 'DT#2019-9-9-12:30:30.9'
)
-
Infinite values (example: '1.7E+400'
)
-
Additional character after a number (example: '2m'
or '3.14'
) are truncated. Additional characters before a number are not permitted.
-
Spaces before (example: ' 3.14'
)
The operand must represent a valid value of the target data type.
NOTE: If the data type of the operand does not match the target type, or if the value exceeds the range of the target data type, then the result depends on the processor type and is therefore undefined.
Conversions from larger types to smaller types may result in loss of information.
CAUTION
|
|
LOSS OF DATA
When converting mismatched data types or when the value being converted is larger than the target data type, be sure that the result is validated within your application.
Failure to follow these instructions can result in injury or equipment damage.
|
Example in IL
Example
|
Conversion result
|
LD 'TRUE'
STRING_TO_BOOL
ST b
|
TRUE
|
Examples in ST
Example
|
Conversion result
|
b := STRING_TO_BOOL('TRUE');
|
TRUE
|
w := STRING_TO_WORD('abc34');
|
0
|
w := STRING_TO_WORD('34abc');
|
34
|
t := STRING_TO_TIME('T#127ms');
|
T#127ms
|
r := STRING_TO_REAL('1.234');
|
1.234
|
bv := STRING_TO_BYTE('500');
|
244
|
Example in FBD
Example
|
Conversion result
|
|
TRUE
|