Jeffrey Bailey

Converting String Numbers to int... problems using atoi()  (parsing numbers from a string)

Discussion created by Jeffrey Bailey on Aug 12, 2008
Latest reply on Aug 13, 2008 by Jeffrey Bailey
Hello,
 
I am helping one of our electrical engineers on a project for the MC9S08SH8CTJ.
 
I would like to pass commands to it from hyperterminal, but for some reason I am having problems using atoi() to convert the ascii character string into a number
 
Code:
int nThree = 0;char *cThree = "3";nThree = atoi(cThree);

 
 
after stepping through the code I can see that it is getting inside the following method in stdlib.c
 
Code:
unsigned long int _strtoul(LIBDEF_ConstStringPtr nptr, LIBDEF_StringPtr *endptr, int base, unsigned int len)

it enters this loop of the previously mentioned function:

Code:
if (base < 37) {    while (CheckCharWithBase(*nptr, base, &val)) {      CHECK_OVERFLOW(lsum, >, (ULONG_MAX - val) / base, ERANGE);      lsum = lsum * base + val;      ++nptr;      if (--len == 0) {        goto done; /*lint !e801 Use of goto is not deprecated */      }    } /* end while */  }

The value for "lsum" is 0, "base" here is 10, "val" is 3, and nptr is pointing to the ascii character "3".  When it does the check overflow, it ends up in another file RTSHC08.c in this block of code, and it does not proceed past the last line here.

Code:
static void _ENTER_BINARY_L(void) {  asm {        _PUSH_REGS        PSHX              ; // @i        PSHH#ifdef __HCS08__        LDHX 6,SP         ; // ra        PSHX        PSHH


 
The command window provides this error:

Error: Attempt to push or pop with SP out of allowed range

 

Please note that I am not really used to C, and I have spent much more time in VB.Net, Java, and C#.  I need to be able to parse integer strings before I can proceed to terminal commands.  Thanks for the help.
 


 
 
 
 
 


Message Edited by jprime on 2008-08-12 08:49 PM

Outcomes