Integer conversion
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! |
Loss of information possible If a larger data type is converted to a smaller data type, information may be lost. |
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. |
The operators convert:
- an integer data type to the specified data type,
- another data type to an integer data type,
- or an integer data type to another integer data type.
You can specify a variable or a literal as the operand.
If a larger data type is converted to a smaller data type, the high-order (front) bytes are truncated. If a smaller data type is converted to a larger data type, the high-order bytes are filled with zeros.
Syntax:
<INT-Datentyp>_TO_<Datentyp> (<Variablenname> | <Literal>)
<Datentyp>_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
<INT-Datentyp>_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
Integral data types:
- BYTE
- WORD, DWORD, LWORD
- SINT, INT, DINT, LINT
- USINT, UINT, UDINT, ULINT
Examples:
Result: nVar is 127.
ST:
nVar := INT_TO_SINT(4223);
If you save the integer number 4223 (16#107f in hexadecimal representation) in a SINT variable, this variable contains the number 127 (16#7f in hexadecimal representation).
FBD:
See also:
- Data types > Integer Data Types