Type
| Function |
Description
| The overloaded function TO_SINT converts an input value of various data types into an output value of the data type SINT (see description of the input for the supported data types). A signed conversion is performed. |
Parameters
| Input
IN
Data type: | The function is available for the following input data types:
- BOOL, BYTE, WORD, DWORD, LWORD
- Integers: INT, DINT, LINT, USINT, UINT, UDINT, ULINT
- Floating point numbers: REAL, LREAL
- STRING, WSTRING
|
Output
OUT
|
Conversion rules
| ANY_BIT conversion
The conversion is done by copying the input value bitwise to the output value, i.e., a binary transfer from the input data type to the output data type is performed.
-
Input size = output size: If the input data type has the same size than the SINT output, the binary transfer results in a signed conversion because the most significant bit of the input value is interpreted as sign bit and transferred to the most significant bit of the integer output which is also considered as sign bit.
-
Input size > output size: If the input data type is bigger than the SINT output (> 8 bits), only the bits 0 to 7 of the input value are copied to the output value.
ANY_INT conversion
The data type conversion is done as follows:
- The source data type is converted to the largest data type of the same generic data type group (for example, an USINT value is converted to an ULINT value or a SINT value to a LINT value). The source value remains unchanged.
- The result is converted to the largest data type of the generic data type group to which the target data type belongs to (for example, an ULINT value is converted to a LINT value in case of an ANY_SIGNED conversion).
- The result is converted to the target data type.
Input size <= output size: If the input data type is smaller or has the same size than the target data type, all bits of the value are copied to the output. For ANY_SIGNED target data types, the most significant bit is interpreted as sign bit. In case of an unsigned integer input value, the "unused" bits in the target data type are set to 0.
Input size > output size: If the input data type is bigger than the target data type, only the bits of the input value that fits into the target data type are copied to the output value (for example, for a TO_DINT conversion the bits 0 to 31 of the input value are copied and for a TO_INT conversion the bits 0 to 15 are stored into the output variable.) For ANY_SIGNED target data types, the most significant bit is interpreted as sign bit.
Example: out := TO_SINT(USINT#255);
- The USINT value USINT#255 is converted to the ULINT value ULINT#255.
- The ULINT value ULINT#255 is converted to the LINT value LINT#255.
- The LINT value LINT#255 is converted to the SINT value SINT#-1.
ANY_REAL conversion
The type conversion is divided into two steps. First the floating point value is rounded to the next higher absolute integer according to standard rounding rules (for example, 2.5 is rounded to 3 and -2.5 is rounded to -3). In the second step the rounded input value is converted to the least significant bits of the output value.
ANY_STRING conversion
- The string value has to be a valid numeric literal of the output data type, otherwise the function returns the value 0 and the runtime error 'Format string failure' occurs (see the note below).
- Additional characters except than whitespaces after or before the numeric literal are not permitted. The function returns the value 0 and generates a 'Format String Failure' if the string value contains unallowed characters.
- All leading and trailing whitespaces in the input string are ignored.ASCII codes regarded as whitespaces:
Character | Value |
Horizontal tab | 9 (09h) |
Line feed | 10 (0Ah) |
Vertical tab | 11 (0Bh) |
Form feed | 12 (0Ch) |
Carriage return | 13 (0Dh) |
Blank | 32 (20h) |
- If the input value is out of the range for the output data type, the overflow value is returned and the runtime error 'Format string failure' occurs (see the note below).
- Binary, octal, hexadecimal and decimal integers are allowed. For example:INT#2#0101 (binary representation of 5)INT#8#606 (octal representation of 390)INT#10#100 (decimal representation of 100)INT#16#00FA (hexadecimal representation of 250)
Note
Controller stop on string error: The controller can be automatically stopped when a string error occurs. This allows you to locate the error in your project. To enable the automatic stop of the controller in case of a string error, activate the button in the Cockpit toolbar of the controller. |
|
Error behavior
| Error behavior
The following table shows the errors which may occur while executing the function and the value returned by the function in the corresponding error case. As described, the value returned by the function in the error case depends on the value of the input (for example, if the input value of data type REAL is not a legal number (NaN), the function returns the value 0).
Error case | Output value RFC/NFC/AXC F 3152 | Output value AXC F 2152 |
Overflow value | Overflow value | Overflow value |
Real value > LINT_MAX (overflow) | 0 | 0 |
Real value < LINT_MIN (overflow) | 0 | 0, 1 for input <= -ULINT_MAX |
Real: NaN | 0 | 0 |
Real: inf+ | 0 | -1 |
Real: Inf- | 0 | 1 |
Non-literal string | 0 | 0 |
String overflow | Overflow value | Overflow value |
|
Examples
| Examples in ST
out := TO_SINT(16#7F); (* results in 127 *)
out := TO_SINT(16#FFFF); (* results in -1 *)
out := TO_SINT(16#AFFFFFF0); (* results in -16 *)
out := TO_SINT(INT#127); (* results in 127 *)
out := TO_SINT(DINT#128); (* results in -128 *)
out := TO_SINT(DINT#129); (* results in -127 *)
out := TO_SINT(UINT#255); (* results in -1 *)
out := TO_SINT(REAL#1.28E+002); (* results in -128 *)
out := TO_SINT('123'); (* results in 123 *)
|
Additional information
| EN/ENO behavior
The POU is only executed if TRUE is applied to the EN input. If EN = FALSE, the POU is not executed and ENO = FALSE indicates the inactivity.
If any of the errors listed under "Error behavior" occurs while executing the function, ENO is set to FALSE.
|