.

整数转换

.
.

提示!

如果类型转换运算符的操作数值超出目标数据类型的值范围,则结果输出取决于处理器类型,因此未定义。例如,当将负操作数值从LREAL 转换为目标数据类型UINT时,就是这种情况。

从较大的数据类型转换为较小的数据类型时,可能会丢失信息。

运算符将整数值转换为指定的数据类型,并返回此类型转换后的值。如果要转换的数字超出范围限制,则该数字的第一个字节将被忽略。

.
调用语法

<变量名> := <整数转换类型运算符> ( <整数操作数> );

<整数转换类型运算符> = <整数数据类型> _TO_ <数据类型>
<整数操作数> = <变量名> | <文字>

<整数数据类型> = 
__UXINT | __XINT | __XWORD | BIT | BYTE | DINT | DWORD | INT | LINT | LWORD | SINT | UDINT | UINT | ULINT | USINT | WORD 
 

.运算符


__UXINT_TO___XINT
__UXINT_TO___XWORD
__UXINT_TO_BIT
__UXINT_TO_BOOL
__UXINT_TO_BYTE
__UXINT_TO_DATE
__UXINT_TO_DINT
__UXINT_TO_DT
__UXINT_TO_DWORD
__UXINT_TO_INT
__UXINT_TO_LDATE
__UXINT_TO_LDT
__UXINT_TO_LINT
__UXINT_TO_LREAL
__UXINT_TO_LTIME
__UXINT_TO_LTOD
__UXINT_TO_LWORD
__UXINT_TO_REAL
__UXINT_TO_SINT
__UXINT_TO_STRING
__UXINT_TO_TIME
__UXINT_TO_TOD
__UXINT_TO_UDINT
__UXINT_TO_UINT
__UXINT_TO_ULINT
__UXINT_TO_USINT
__UXINT_TO_WORD
__UXINT_TO_WSTRING

__XINT_TO___UXINT
__XINT_TO___XWORD
__XINT_TO_BIT
__XINT_TO_BOOL
__XINT_TO_BYTE
__XINT_TO_DATE
__XINT_TO_DINT
__XINT_TO_DT
__XINT_TO_DWORD
__XINT_TO_INT
__XINT_TO_LDATE
__XINT_TO_LDT
__XINT_TO_LINT
__XINT_TO_LREAL
__XINT_TO_LTIME
__XINT_TO_LTOD
__XINT_TO_LWORD
__XINT_TO_REAL
__XINT_TO_SINT
__XINT_TO_STRING
__XINT_TO_TIME
__XINT_TO_TOD
__XINT_TO_UDINT
__XINT_TO_UINT
__XINT_TO_ULINT
__XINT_TO_USINT
__XINT_TO_WORD
__XINT_TO_WSTRING

__XWORD_TO_UXINT
__XWORD_TO_XINT
__XWORD_TO_BIT
__XWORD_TO_BOOL
__XWORD_TO_BYTE
__XWORD_TO_DATE
__XWORD_TO_DINT
__XWORD_TO_DT
__XWORD_TO_DWORD
__XWORD_TO_INT
__XWORD_TO_LDATE
__XWORD_TO_LDT
__XWORD_TO_LINT
__XWORD_TO_LREAL
__XWORD_TO_LTIME
__XWORD_TO_LTOD
__XWORD_TO_LWORD
__XWORD_TO_REAL
__XWORD_TO_SINT
__XWORD_TO_STRING
__XWORD_TO_TIME
__XWORD_TO_TOD
__XWORD_TO_UDINT
__XWORD_TO_UINT
__XWORD_TO_ULINT
__XWORD_TO_USINT
__XWORD_TO_WORD
__XWORD_TO_WSTRING

BIT_TO___UXINT
BIT_TO___XINT
BIT_TO___XWORD
BIT_TO_BOOL
BIT_TO_BYTE
BIT_TO_DATE
BIT_TO_DINT
BIT_TO_DT
BIT_TO_DWORD
BIT_TO_INT
BIT_TO_LDATE
BIT_TO_LDT
BIT_TO_LINT
BIT_TO_LREAL
BIT_TO_LTIME
BIT_TO_LTOD
BIT_TO_LWORD
BIT_TO_REAL
BIT_TO_SINT
BIT_TO_STRING
BIT_TO_TIME
BIT_TO_TOD
BIT_TO_UDINT
BIT_TO_UINT
BIT_TO_ULINT
BIT_TO_USINT
BIT_TO_WORD
BIT_TO_WSTRING

BYTE_TO___UXINT
BYTE_TO___XINT
BYTE_TO___XWORD
BYTE_TO_BOOL
BYTE_TO_BIT
BYTE_TO_DATE
BYTE_TO_DINT
BYTE_TO_DT
BYTE_TO_DWORD
BYTE_TO_INT
BYTE_TO_LDATE
BYTE_TO_LDT
BYTE_TO_LINT
BYTE_TO_LREAL
BYTE_TO_LTIME
BYTE_TO_LTOD
BYTE_TO_LWORD
BYTE_TO_REAL
BYTE_TO_SINT
BYTE_TO_STRING
BYTE_TO_TIME
BYTE_TO_TOD
BYTE_TO_UDINT
BYTE_TO_UINT
BYTE_TO_ULINT
BYTE_TO_USINT
BYTE_TO_WORD
BYTE_TO_WSTRING

DINT_TO___UXINT
DINT_TO___XINT
DINT_TO___XWORD
DINT_TO_BOOL
DINT_TO_BIT
DINT_TO_BYTE
DINT_TO_DATE
DINT_TO_DT
DINT_TO_DWORD
DINT_TO_INT
DINT_TO_LDATE
DINT_TO_LDT
DINT_TO_LINT
DINT_TO_LREAL
DINT_TO_LTIME
DINT_TO_LTOD
DINT_TO_LWORD
DINT_TO_REAL
DINT_TO_SINT
DINT_TO_STRING
DINT_TO_TIME
DINT_TO_TOD
DINT_TO_UDINT
DINT_TO_UINT
DINT_TO_ULINT
DINT_TO_USINT
DINT_TO_WORD
DINT_TO_WSTRING

DWORD_TO___UXINT
DWORD_TO___XINT
DWORD_TO___XWORD
DWORD_TO_BIT
DWORD_TO_BOOL
DWORD_TO_BYTE
DWORD_TO_DATE
DWORD_TO_DINT
DWORD_TO_DT
DWORD_TO_INT
DWORD_TO_LDATE
DWORD_TO_LDT
DWORD_TO_LINT
DWORD_TO_LREAL
DWORD_TO_LTIME
DWORD_TO_LTOD
DWORD_TO_LWORD
DWORD_TO_REAL
DWORD_TO_SINT
DWORD_TO_STRING
DWORD_TO_TIME
DWORD_TO_TOD
DWORD_TO_UDINT
DWORD_TO_UINT
DWORD_TO_ULINT
DWORD_TO_USINT
DWORD_TO_WORD
DWORD_TO_WSTRING

INT_TO___UXINT
INT_TO___XINT
INT_TO___XWORD
INT_TO_BIT
INT_TO_BOOL
INT_TO_BYTE
INT_TO_DATE
INT_TO_DINT
INT_TO_DT
INT_TO_DWORD
INT_TO_LDATE
INT_TO_LDT
INT_TO_LINT
INT_TO_LREAL
INT_TO_LTIME
INT_TO_LTOD
INT_TO_LWORD
INT_TO_REAL
INT_TO_SINT
INT_TO_STRING
INT_TO_TIME
INT_TO_TOD
INT_TO_UDINT
INT_TO_UINT
INT_TO_ULINT
INT_TO_USINT
INT_TO_WORD
INT_TO_WSTRING

LINT_TO___UXINT
LINT_TO___XINT
LINT_TO___XWORD
LINT_TO_BIT
LINT_TO_BOOL
LINT_TO_BYTE
LINT_TO_DATE
LINT_TO_DINT
LINT_TO_DT
LINT_TO_DWORD
LINT_TO_INT
LINT_TO_LDATE
LINT_TO_LDT
LINT_TO_LREAL
LINT_TO_LTIME
LINT_TO_LTOD
LINT_TO_LWORD
LINT_TO_REAL
LINT_TO_SINT
LINT_TO_STRING
LINT_TO_TIME
LINT_TO_TOD
LINT_TO_UDINT
LINT_TO_UINT
LINT_TO_ULINT
LINT_TO_USINT
LINT_TO_WORD
LINT_TO_WSTRING

LWORD_TO___UXINT
LWORD_TO___XINT
LWORD_TO___XWORD
LWORD_TO_BIT
LWORD_TO_BOOL
LWORD_TO_BYTE
LWORD_TO_DATE
LWORD_TO_DINT
LWORD_TO_DT
LWORD_TO_DWORD
LWORD_TO_INT
LWORD_TO_LDATE
LWORD_TO_LDT
LWORD_TO_LINT
LWORD_TO_LREAL
LWORD_TO_LTIME
LWORD_TO_LTOD
LWORD_TO_REAL
LWORD_TO_SINT
LWORD_TO_STRING
LWORD_TO_TIME
LWORD_TO_TOD
LWORD_TO_UDINT
LWORD_TO_UINT
LWORD_TO_ULINT
LWORD_TO_USINT
LWORD_TO_WORD
LWORD_TO_WSTRING

SINT_TO___UXINT
SINT_TO___XINT
SINT_TO___XWORD
SINT_TO_BIT
SINT_TO_BOOL
SINT_TO_BYTE
SINT_TO_DATE
SINT_TO_DINT
SINT_TO_DT
SINT_TO_DWORD
SINT_TO_INT
SINT_TO_LDATE
SINT_TO_LDT
SINT_TO_LINT
SINT_TO_LREAL
SINT_TO_LTIME
SINT_TO_LTOD
SINT_TO_LWORD
SINT_TO_REAL
SINT_TO_STRING
SINT_TO_TIME
SINT_TO_TOD
SINT_TO_UDINT
SINT_TO_UINT
SINT_TO_ULINT
SINT_TO_USINT
SINT_TO_WORD
SINT_TO_WSTRING

UDINT_TO___UXINT
UDINT_TO___XINT
UDINT_TO___XWORD
UDINT_TO_BIT
UDINT_TO_BOOL
UDINT_TO_BYTE
UDINT_TO_DATE
UDINT_TO_DINT
UDINT_TO_DT
UDINT_TO_DWORD
UDINT_TO_INT
UDINT_TO_LDATE
UDINT_TO_LDT
UDINT_TO_LINT
UDINT_TO_LREAL
UDINT_TO_LTIME
UDINT_TO_LTOD
UDINT_TO_LWORD
UDINT_TO_REAL
UDINT_TO_SINT
UDINT_TO_STRING
UDINT_TO_TIME
UDINT_TO_TOD
UDINT_TO_UINT
UDINT_TO_ULINT
UDINT_TO_USINT
UDINT_TO_WORD
UDINT_TO_WSTRING

UINT_TO___UXINT
UINT_TO___XINT
UINT_TO___XWORD
UINT_TO_BIT
UINT_TO_BOOL
UINT_TO_BYTE
UINT_TO_DATE
UINT_TO_DINT
UINT_TO_DT
UINT_TO_DWORD
UINT_TO_INT
UINT_TO_LDATE
UINT_TO_LDT
UINT_TO_LINT
UINT_TO_LREAL
UINT_TO_LTIME
UINT_TO_LTOD
UINT_TO_LWORD
UINT_TO_REAL
UINT_TO_SINT
UINT_TO_STRING
UINT_TO_TIME
UINT_TO_TOD
UINT_TO_UDINT
UINT_TO_ULINT
UINT_TO_USINT
UINT_TO_WORD
UINT_TO_WSTRING

ULINT_TO___UXINT
ULINT_TO___XINT
ULINT_TO___XWORD
ULINT_TO_BIT
ULINT_TO_BOOL
ULINT_TO_BYTE
ULINT_TO_DATE
ULINT_TO_DINT
ULINT_TO_DT
ULINT_TO_DWORD
ULINT_TO_INT
ULINT_TO_LDATE
ULINT_TO_LDT
ULINT_TO_LINT
ULINT_TO_LREAL
ULINT_TO_LTIME
ULINT_TO_LTOD
ULINT_TO_LWORD
ULINT_TO_REAL
ULINT_TO_SINT
ULINT_TO_STRING
ULINT_TO_TIME
ULINT_TO_TOD
ULINT_TO_UDINT
ULINT_TO_UINT
ULINT_TO_USINT
ULINT_TO_WORD
ULINT_TO_WSTRING

USINT_TO___XINT
USINT_TO___XINT
USINT_TO___XWORD
USINT_TO_BIT
USINT_TO_BOOL
USINT_TO_BYTE
USINT_TO_DATE
USINT_TO_DINT
USINT_TO_DT
USINT_TO_DWORD
USINT_TO_INT
USINT_TO_LDATE
USINT_TO_LDT
USINT_TO_LINT
USINT_TO_LREAL
USINT_TO_LTIME
USINT_TO_LTOD
USINT_TO_LWORD
USINT_TO_REAL
USINT_TO_SINT
USINT_TO_STRING
USINT_TO_TIME
USINT_TO_TOD
USINT_TO_UDINT
USINT_TO_UINT
USINT_TO_ULINT
USINT_TO_WORD
USINT_TO_WSTRING

WORD_TO___XINT
WORD_TO___XINT
WORD_TO___XWORD
WORD_TO_BIT
WORD_TO_BOOL
WORD_TO_BYTE
WORD_TO_DATE
WORD_TO_DINT
WORD_TO_DT
WORD_TO_DWORD
WORD_TO_INT
WORD_TO_LDATE
WORD_TO_LDT
WORD_TO_LINT
WORD_TO_LREAL
WORD_TO_LTIME
WORD_TO_LTOD
WORD_TO_LWORD
WORD_TO_REAL
WORD_TO_SINT
WORD_TO_STRING
WORD_TO_TIME
WORD_TO_TOD
WORD_TO_UDINT
WORD_TO_UINT
WORD_TO_ULINT
WORD_TO_USINT
WORD_TO_WSTRING
 
.
转换为字符串

提示!

转换为STRINGWSTRING的字符串操作

将类型转换为STRINGWSTRING时,键入的值将作为字符串左对齐,并且如果过长则被截断。因此,为类型转换运算符<>_TO_STRING<>_TO_WSTRING声明返回变量的长度应足够长,以便在不进行任何操作的情况下字符串有足够的空间。

将值转换为STRINGWSTRING类型的字符串的运算符需要与目标数据类型匹配的操作数。

.

.例子

.
例子
.

.ST实现语言

当较大的数据类型转换为较小的数据类型时,较高的(前)字节将被截断。当更小的数据类型转换为更大的数据类型时,更多的高阶字节将填满零。

FUNCTION_BLOCK FB_ConvertIntegersFromInt
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
    uxiReturn: __UXINT;
    xiReturn: __XINT;
    xwReturn: __XWORD;
    bitReturn: BIT;
    xReturn: BOOL;
    bReturn: BYTE;
    dateReturn: DATE;
    diReturn: DINT;
    dtReturn: DATE_AND_TIME;
    dwReturn: DWORD;    
    liReturn: LINT;
    lrReturn: LREAL;
    lwReturn: LWORD;
    siReturn: SINT;
    sReturn: STRING;
    timReturn: TIME;
    todReturn: TIME_OF_DAY;
    udiReturn: UDINT;
    uiReturn: UINT;
    usiReturn: USINT;
    wReturn: WORD;
    wsReturn: WSTRING;
    uliReturn: ULINT;
END_VAR

uxiReturn := INT_TO___UXINT(127);
xiReturn := INT_TO___XINT(127);
xwReturn := INT_TO___XWORD(127);
bitReturn := INT_TO_BIT(127);
xReturn := INT_TO_BOOL(127);
bReturn := INT_TO_BYTE(127);
dateReturn := INT_TO_DATE(127);
diReturn := INT_TO_DINT(127);
dtReturn := INT_TO_DT(127);
dwReturn := INT_TO_DWORD(127);
liReturn := INT_TO_LINT(127);
lrReturn := INT_TO_LREAL(127);
lwReturn := INT_TO_LWORD(127);
siReturn := INT_TO_SINT(127);
sReturn := INT_TO_STRING(127);
timReturn := INT_TO_TIME(127);
todReturn := INT_TO_TOD(127);
udiReturn := INT_TO_UDINT(127);
uiReturn := INT_TO_UINT(127);
uliReturn := INT_TO_ULINT(127);
usiReturn := INT_TO_USINT(127);
wReturn := INT_TO_WORD(127);
wsReturn := INT_TO_WSTRING(127);

FUNCTION_BLOCK FB_ConvertIntegersToInt
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
    iReturn_uxi: INT;
    iReturn_xi: INT;
    iReturn_xw: INT;
    iReturn_bit: INT;
    iReturn_bool: INT;
    iReturn_b: INT;
    iReturn_d: INT;
    iReturn_di: INT;
    iReturn_dt: INT;
    iReturn_dw: INT;
    iReturn_li: INT;
    iReturn_lr: INT;
    iReturn_lw: INT;
    iReturn_r: INT;
    iReturn_si: INT;
    iReturn_s: INT;
    iReturn_tim: INT;
    iReturn_tod: INT;
    iReturn_tod_0: INT;
    iReturn_udi: INT;
    iReturn_ui: INT;
    iReturn_uli: INT;
    iReturn_usi: INT;
    iReturn_w: INT;
    iReturn_ws: INT;
END_VAR

iReturn_uxi := __UXINT_TO_INT(18446744073709551615);
iReturn_xi := __XINT_TO_INT(9223372036854775807);
iReturn_xw := __XWORD_TO_INT(16#FFFF_FFFF_FFFF_FFFF);
iReturn_bit := BIT_TO_INT(1);
iReturn_bool := BOOL_TO_INT(TRUE);
iReturn_b := BYTE_TO_INT(2#1111_0000);
iReturn_d := DATE_TO_INT(DATE#2019-9-13);
iReturn_di := DINT_TO_INT(2147483647);
iReturn_dt := DT_TO_INT(DT#1979-1-1-00:00:00);
iReturn_dw := DWORD_TO_INT(16#FFFF_FFFF);
// iReturn_i := INT_TO_<>(iData_12);
iReturn_li := LINT_TO_INT(9223372036854775807);
iReturn_lr := LREAL_TO_INT(1.7976931348623157E+30);
iReturn_lw := LWORD_TO_INT(16#FFFF_FFFF_FFFF_FFFF);
iReturn_r := REAL_TO_INT(3.402823E+38);
iReturn_si := SINT_TO_INT(127);
iReturn_s := STRING_TO_INT('127');
iReturn_tim := TIME_TO_INT(T#49D17H2M47S295MS);
iReturn_tod := TOD_TO_INT(TOD#23:59:59.999);
iReturn_tod_0 := TOD_TO_INT(TOD#1:1:1.001);
iReturn_udi := UDINT_TO_INT(4294967295);
iReturn_ui := UINT_TO_INT(65535);
iReturn_uli := ULINT_TO_INT(18446744073709551615);
iReturn_usi := USINT_TO_INT(255);
iReturn_w := WORD_TO_INT(16#FFFF); 
iReturn_ws := WSTRING_TO_INT("1234567890");

PROGRAM PLC_PRG
VAR
    fbConvertIntegersFromInt : FB_ConvertIntegersFromInt;
    fbConvertIntegersToInt : FB_ConvertIntegersToInt;
END_VAR

fbConvertIntegersFromInt();
fbConvertIntegersToInt();
 
.

.FBD实现语言