Topic: DMD0027

Help File Version: 2.10.0.636

STR2INT - Convert String To Integer


The Convert String to Integer (STR2INT) instruction is used to convert the contents of the specified input string from ASCII text to an integer value. Any leading whitespace characters will be automatically skipped.

 

For example, STR2INT will convert a string that contains the characters "12345678" to the integer value 12,345,678.

 

 


Parameters:

Note: Use the F9 key or click the 'three dot box' at the right edge of the parameter field to open the Default Element Selection Tool (the Element Picker or the Element Browser) or use the Down-Arrow key (Auto-Complete) on any parameter field to see a complete list of the memory locations that are valid for that parameter of the instruction.

 

Input String- the String that contains the data to be converted. This can be any of the system-defined Short Strings (SS Block), or system-defined Long Strings (SL Block), or any readable String element.

 

Valid input characters are determined by the input radix as follows:

    • Decimal: '0' - '9'

    • Hexadecimal: '0' - '9', 'A' - 'F'

    • Octal: '0' - '7'

    • Implied Base: same as above

    • Other: the base determines the valid characters
      For example: valid characters for base 2 are '0' and '1', valid characters for base 36 are '0' - '9', 'A' - 'Z', and 'a' - 'z'.

 

If an invalid character is found the valid characters up to that point will be converted. For example, if the input String contains the text "1234x5678", the conversion will result in the value 1234. See the optional parameter Index where Stopped for information on how to handle this condition.

 


Input Radix - specifies the radix (or number base) of the input value which in turn defines the output conversion to be done. For example, assume the input string contains the characters "10", if the Input Radix is set to Decimal, the output value will be 10, if the Input Radix is set to Hexadecimal, the output value will be 16, if the Input Radix is set to 2 the value will be 2.

 

  • Decimal, Hexadecimal and Octal - select the normal Base 10, Base 16, or Base 8 numbering.

     

  • Implied Base (leading "0x" :hex, "0" :octal, "1-9" :decimal) - select this option if the Input Radix information is part of the Input String. For example, if the Input String contains "0x12C7", the Input Radix information "0x" is already part of the Input String.
     

  • Other (2-36) - select this option to specify an Input Radix not listed above, then enter the number base in the right-hand field.

 


Numeric Output - designates where to store the converted integer value. This can be any writable numeric location.

 

Index Where Stopped - if enabled, this designates a writable numeric location that will contain the offset into the Input String after valid digits are found where the conversion process stopped. The following examples display some of the conditions that will cause the conversion to stop and set the Index Where Stopped value.

 

The input string (SS0) is empty

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

<empty string>

0

-1

STR2INT input string is empty

< empty string>

 

The input string (SS0) contains the valid string "10"

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

"10"

10

2

""

""

 

The input string (SS0) contains the valid string "   10   " which has 3 leading spaces that are automatically skipped, Index Where Stopped is set to first space after '0'

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

"   10   "

10

5

""

""

 

The input string (SS0) contains the valid string "    1F    " which has 3 leading spaces that are automatically skipped, Index Where Stopped is set to  'F'

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

   1F   "

1

4

""

""

 

The input string (SS0) contains the valid string "    1F    " but change the base to 36 ( which has 'a' - 'z' as valid characters) , the 3 leading spaces are automatically skipped, Index Where Stopped is set to first space after 'F'

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

"   1F   "

51

5

""

""

 

The input string (SS0) contains the invalid string "    @    "  

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

"   @   "

0

-1

"STR2INT input string couldn't be converted to an integer value."

""

 


Status Display:

 

The Status display of the Convert String to Integer instruction will only display as many of the characters of the Input String as will fit within the borders of the instruction, typically this is about 50 characters.

 


See Also:

GRAY - Gray Code to Integer

 

SCALE - Scale Value

 

SEG - Hex / BCD to 7 Segment Display

 

SWAPB - Swap Bytes

 


STR2REAL - Convert String to Real

 

STR2INT - Convert String to Integer

 


STRCASE - Convert String to UPPER / lower Case

 

STRCLEAR - Clear Strings

 

STRCMP - String Compare

 

STRCOPY - Copy String

 

STRCOPYR - Copy a Range of Strings


STRDELETE - Delete Substring

 

STRFIND - Find within String

 

STRINSERT - Insert Substring

 

STRPRINT - Print to String

 

STRSUB - Get Substring

 

STRTRIM - Trim Whitespace

 

STRTRUNC - Truncate String



Related Topics:

String Functions Overview
 

Scripting Language Reference

 


Rung Example: