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

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

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

4,053 次查看
jprime
Contributor I
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
标签 (1)
0 项奖励
回复
2 回复数

1,132 次查看
CompilerGuru
NXP Employee
NXP Employee
>Error: Attempt to push or pop with SP out of allowed range

means: increase the stack size in the prm file, sounds like it runs out of stack space.

(or use another atoi implementation (I think there have been some in the past in this forum), or recompile it with overflow checking disabled)

Daniel
0 项奖励
回复

1,132 次查看
jprime
Contributor I
That did it!  Thank you.
0 项奖励
回复