Use a two-boundary scan: locate the first character that converts successfully, continue until conversion fails, then pass that contiguous substring to STRNG_I. This handles a digit group whose position and length vary without attempting to convert the surrounding text.
Available Siemens string conversion functions
| Function | Identifier | Result type |
|---|---|---|
| STRNG_I | FC38 | Integer |
| STRNG_DI | FC37 | Double integer |
| STRNG_R | FC39 | Real |
The conversion input must contain a valid numeric string. Therefore, isolate the number with MID before converting it when the source also contains nonnumeric characters.
Find the first contiguous digit group
- Read the source length with
LEN. - Starting at position
1, extract one character withMID(IN := MyString, L := 1, P := position). - Test that character with
STRNG_I. A successful conversion marks the beginning of the first numeric group. - Continue testing consecutive characters until conversion fails or the scan passes the string length.
- Calculate the substring length as
end position - start position, extract it withMID, and convert it withSTRNG_I.
The stated requirement has adjacent numeric characters, so the first failed character after scanning starts terminates the group. The evidence does not define whether a leading sign must be recognized; add explicit sign handling only if signed values are required.
Extract and convert the substring
iVal := STRNG_I(
S := MID(
IN := MyString,
L := StrEnd - StrStart,
P := StrStart
)
);
For a known layout, MID(IN := YourString, L := 5, P := 2) selects positions 2 through 6 inclusive. For a variable layout, derive P and L from the scan rather than assigning fixed values.
Multiple values, limits, and verification
The supplied FB103 implementation repeats the scan and stores converted groups in iVal[1..MAXVALS], with MAXVALS := 10. It sets NUMVALS to the number extracted and stops at ten values. However, iVal is declared in the internal VAR area rather than VAR_OUTPUT; expose or copy the array if calling code must consume the extracted values.
Verify three termination cases: a group followed by text, a group ending at the final string character, and a string containing no convertible group. Also confirm that a conversion failure does not reuse an earlier value and that array indexing never exceeds MAXVALS.
FAQ
How do I extract the first number from a Siemens SCL string?
Scan one character at a time with MID and test it with STRNG_I. Record the first successful position, stop at the next failure, and convert the resulting substring.
Which Siemens function converts a string to an integer?
Use STRNG_I (FC38) for an integer. The available alternatives are STRNG_DI (FC37) for a double integer and STRNG_R (FC39) for a real value.
Why does NUMVALS update but the extracted array remain unavailable?
In the supplied FB103, iVal is an internal variable while only NUMVALS is an output. Expose or copy iVal so external logic can read the extracted values.