如果类型转换运算符的操作数值超出目标数据类型的值范围,则结果输出取决于处理器类型,因此未定义。例如,当将负操作数值从LREAL 转换为目标数据类型UINT时,就是这种情况。 从较大的数据类型转换为较小的数据类型时,可能会丢失信息。 |
运算符将字符串(STRING或WSTRING)转换为指定的目标数据类型,并返回类型转换后的值。
根据IEC 61131-3标准,只有当操作数匹配目标数据类型时,才可能实现具有有意义结果的转换。如果操作数的值与目标数据类型的有效常数(文字)相对应,就是这种情况。
.可转换字符串包含:
<变量名> := <字符串 to 运算符> ( <操作数> );
<操作数> = <变量名> | <文字>
.运算符
STRING_TO___UXINT
STRING_TO___XINT
STRING_TO___XWORD
STRING_TO_BIT
STRING_TO_BOOL
STRING_TO_BYTE
STRING_TO_DATE
STRING_TO_DINT
STRING_TO_DT
STRING_TO_DWORD
STRING_TO_INT
STRING_TO_LDATE
STRING_TO_LDT
STRING_TO_LINT
STRING_TO_LREAL
STRING_TO_LTIME
STRING_TO_LWORD
STRING_TO_LTIME
STRING_TO_LTOD
STRING_TO_REAL
STRING_TO_SINT
STRING_TO_TIME
STRING_TO_TOD
STRING_TO_UDINT
STRING_TO_UINT
STRING_TO_ULINT
STRING_TO_USINT
STRING_TO_WORD
STRING_TO_WSTRING
WSTRING_TO___UXINT
WSTRING_TO___XINT
WSTRING_TO___XWORD
WSTRING_TO_BIT
WSTRING_TO_BOOL
WSTRING_TO_BYTE
WSTRING_TO_DATE
WSTRING_TO_DINT
WSTRING_TO_DT
WSTRING_TO_DWORD
WSTRING_TO_INT
WSTRING_TO_LDATE
WSTRING_TO_LDT
WSTRING_TO_LINT
WSTRING_TO_LREAL
WSTRING_TO_LTIME
WSTRING_TO_LTOD
WSTRING_TO_LWORD
WSTRING_TO_LTIME
WSTRING_TO_REAL
WSTRING_TO_SINT
WSTRING_TO_STRING
WSTRING_TO_TIME
WSTRING_TO_TOD
WSTRING_TO_UDINT
WSTRING_TO_UINT
WSTRING_TO_ULINT
WSTRING_TO_USINT
WSTRING_TO_STRING
WSTRING_TO_WORD
操作符 STRING_TO_BOOL: 只有当操作数值为’TRUE’或’true’时,才返回TRUE值。另一方面, 'True'返回FALSE。
运算符 WSTRING_TO_BOOL: 只有当操作数值为”TRUE””或”true”时,才返回TRUE值。另一方面,true返回FALSE。
.ST实现语言
FUNCTION_BLOCK FB_ConvertStrings
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
xReturn_0: BOOL;
xReturn_1: BOOL;
dateReturn: DATE;
dtReturn: DATE_AND_TIME;
iReturn: INT;
lrReturn: LREAL;
lrReturn_0: LREAL;
lwReturn: LWORD;
lwReturn_0: LWORD;
lwReturn_1: LWORD;
ltReturn: LTIME;
ltReturn_0: LTIME;
ltReturn_1: LTIME;
ltReturn_2: LTIME;
rReturn: REAL;
rReturn_0: REAL;
timReturn: TIME;
timReturn0: TIME;
timReturn1: TIME;
timReturn2: TIME;
todReturn: TIME_OF_DAY;
todReturn0: TIME_OF_DAY;
todReturn1: TIME_OF_DAY;
todReturn2: TIME_OF_DAY;
uliReurn: ULINT;
uliReurn_0: ULINT;
uliReurn_1: ULINT;
wReturn: WORD;
wReturn_0: WORD;
wReturn_1: WORD;
wstrReturn: WSTRING;
wstrReturn_0: WSTRING;
END_VAR
xReturn_0 := STRING_TO_BOOL('FALSE');
xReturn_1 := STRING_TO_BOOL('TRUE');
dateReturn := STRING_TO_DATE('DATE#2019-9-9');
dtReturn := STRING_TO_DT('DT#2019-9-9-1:1:1.1');
iReturn := STRING_TO_INT('123abc');
lrReturn := STRING_TO_LREAL('4.94E-323');
lrReturn_0 := STRING_TO_LREAL('1.7E+308');
lwReturn := STRING_TO_LWORD('16#FFFF_FFFF_FFFF_FFFF');
lwReturn_0 := STRING_TO_LWORD('16#0123456789ABCDEF');
lwReturn_1 := STRING_TO_LWORD('16#0123456789ABCDEF');
ltReturn := STRING_TO_LTIME('LTIME#213503d23h34m33s709ms551us615ns');
ltReturn_0 := STRING_TO_LTIME('LTIME#0ns');
ltReturn_1 := STRING_TO_LTIME('LTIME#1ms');
ltReturn_2 := STRING_TO_LTIME('LTIME#2s');
rReturn := STRING_TO_REAL('6.543e21');
rReturn_0 := STRING_TO_REAL('1.234');
timReturn := STRING_TO_TIME('T#5d4h3m2s');
timReturn0 := STRING_TO_TIME('TIME#1s');
timReturn1 := STRING_TO_TIME('1s');
timReturn2 := STRING_TO_TIME('TIME#5s');
todReturn := STRING_TO_TOD('TOD#12:0:0.1');
todReturn0 := STRING_TO_TOD('TOD#0:0:0.0');
todReturn1 := STRING_TO_TOD('20:15');
todReturn2 := STRING_TO_TOD('TOD#20:15');
uliReurn := STRING_TO_ULINT('18446744073709551615');
uliReurn_0 := STRING_TO_ULINT('1');
uliReurn_1 := STRING_TO_ULINT('0');
wReturn := STRING_TO_WORD('16#FFFF_0000');
wReturn_0 := STRING_TO_WORD('34abc');
wReturn_1 := STRING_TO_WORD('16#34abc');
wstrReturn := STRING_TO_WSTRING('Hello World!');
wstrReturn_0 := STRING_TO_WSTRING('123456789');
.ST中的WSTRING转换
FUNCTION_BLOCK FB_ConvertWstrings
VAR
xReturn_0: BOOL;
xReturn_1: BOOL;
dateReturn: DATE;
dtReturn: DATE_AND_TIME;
iReturn: INT;
lrReturn: LREAL;
lrReturn_0: LREAL;
lwReturn: LWORD;
lwReturn_0: LWORD;
lwReturn_1: LWORD;
ltReturn: LTIME;
ltReturn_0: LTIME;
ltReturn_1: LTIME;
ltReturn_2: LTIME;
rReturn: REAL;
rReturn_0: REAL;
timReturn: TIME;
timReturn0: TIME;
timReturn1: TIME;
timReturn2: TIME;
todReturn: TIME_OF_DAY;
todReturn0: TIME_OF_DAY;
todReturn1: TIME_OF_DAY;
todReturn2: TIME_OF_DAY;
uliReurn: ULINT;
uliReurn_0: ULINT;
uliReurn_1: ULINT;
wReturn: WORD;
wReturn_0: WORD;
wReturn_1: WORD;
wstrReturn: WSTRING;
wstrReturn_0: WSTRING;
END_VAR
xReturn_0 := WSTRING_TO_BOOL("FALSE");
xReturn_1 := WSTRING_TO_BOOL("TRUE");
dateReturn := WSTRING_TO_DATE("DATE#2019-9-9");
dtReturn := WSTRING_TO_DT("DT#2019-9-9-1:1:1.1");
iReturn := WSTRING_TO_INT("123abc");
lrReturn := WSTRING_TO_LREAL("4.94E-323");
lrReturn_0 := WSTRING_TO_LREAL("1.7E+308");
lwReturn := WSTRING_TO_LWORD("16#FFFF_FFFF_FFFF_FFFF");
lwReturn_0 := WSTRING_TO_LWORD("16#0123456789ABCDEF");
lwReturn_1 := WSTRING_TO_LWORD("16#0123456789ABCDEF");
ltReturn := WSTRING_TO_LTIME("LTIME#213503d23h34m33s709ms551us615ns");
ltReturn_0 := WSTRING_TO_LTIME("LTIME#0ns");
ltReturn_1 := WSTRING_TO_LTIME("LTIME#1ms");
ltReturn_2 := WSTRING_TO_LTIME("LTIME#2s");
rReturn := WSTRING_TO_REAL("6.543e21");
rReturn_0 := WSTRING_TO_REAL("1.234");
timReturn := WSTRING_TO_TIME("T#5d4h3m2s");
timReturn0 := WSTRING_TO_TIME("TIME#1s");
timReturn1 := WSTRING_TO_TIME("1s");
timReturn2 := WSTRING_TO_TIME("TIME#5s");
todReturn := WSTRING_TO_TOD("TOD#12:0:0.1");
todReturn0 := WSTRING_TO_TOD("TOD#0:0:0.0");
todReturn1 := WSTRING_TO_TOD("20:15");
todReturn2 := WSTRING_TO_TOD("TOD#20:15");
uliReurn := WSTRING_TO_ULINT("18446744073709551615");
uliReurn_0 := WSTRING_TO_ULINT("1");
uliReurn_1 := WSTRING_TO_ULINT("0");
wReturn := WSTRING_TO_WORD("16#FFFF_0000");
wReturn_0 := WSTRING_TO_WORD("34abc");
wReturn_1 := WSTRING_TO_WORD("16#34abc");