Str_ParseNbr_Int32S()

Parses a 32-bit signed integer from a string.

Files

lib_str.h/lib_str.c

Prototype

          CPU_INT32S  Str_ParseNbr_Int32S (CPU_CHAR     *pstr,
                                           CPU_CHAR    **pstr_end,
                                           CPU_INT08U    nbr_base);

Arguments

pstr

Pointer to string.

pstr_end

Pointer to a variable to …

Return a pointer to first character following the integer string, if no errors;

Return a pointer to pstr, if any errors.

nbr_base

Base of number to parse:

0 (zero); the actual base will be determined from the integer string:

If the integer string begins with "0x" or "0X", the base is 16.
If the integer string begins with "0" but not "0x"/"0X", the base is 8.
Otherwise, the base is 10.

Integer between 2 and 36, inclusive.

Returned Value

Parsed integer, if integer was successfully parsed and neither overflowed or underflowed.

DEF_INT_32S_MAX_VAL, if parsed integer overflowedto the most positivevalue.

DEF_INT_32S_MIN_VAL, if parsed integer underflowedto the most negativevalue.

0, otherwise.

Required Configuration

None.

Notes / Warnings

The input string consists of:

  • An initial, possibly empty, sequence of white-space characters.
  • An optional sign character ('-' or '+').
  • A sequence of characters representing an integer in some radix:
    • If the base is 16, one of the optional character sequences "0x" or "0X";
    • A sequence of letters and digits. The letters from 'a'/'A' to 'z'/'Z' are assigned the values 10 through 35, respectively; but only letters and digits whose assigned values are less than that of the base are valid.
  • A string of invalid or unrecognized characters, perhaps including a terminating NULL character.

Return integer value and next string pointer (pstr_end) should be used to diagnose parse success or failure. Examples:

Valid parse string integer:

pstr = " ABCDE xyz"
nbr_base = 16
nbr = 703710
pstr_next = " xyz"

Invalid parse string integer:

pstr = " ABCDE"
nbr_base = 10
nbr = 0
pstr_next = pstr = " ABCDE"

Valid hexadecimal parse string integer:

pstr = " 0xGABCDE"
nbr_base = 16
nbr = 0
pstr_next = "xGABCDE"

Valid decimal parse string integer ('0x' prefix ignored following invalid hexadecimal characters):

pstr = " 0xGABCDE"
nbr_base = 0
nbr = 0
pstr_next = "xGABCDE"

Valid decimal parse string integer ('0' prefix ignored following invalid octal characters):

pstr = " 0GABCDE"
nbr_base = 0
nbr = 0
pstr_next = "GABCDE"

Parse string integer overflow:

pstr = " 12345678901234567890*123456"
nbr_base = 10
nbr = DEF_INT_32S_MAX_VAL
pstr_next = "*123456"

Parse string integer underflow:

pstr = " -12345678901234567890*123456"
nbr_base = 10
nbr = DEF_INT_32S_MIN_VAL
pstr_next = "*123456"

Example Usage

          CPU_INT32S   nbr;
          CPU_CHAR    *pstr_end;


          nbr = Str_ParseNbr_Int32S((CPU_CHAR  *)"-1234534*-23434>345344",
                                    (CPU_CHAR **)&pstr_end,
                                    (CPU_INT08U ) 10u);