• C

string to int - easy question lots of points

How in c do I convert a string to an int.
ie '1' too 1.  Thus allowing to use it in a sum.

A simple question but I need an answer fast.

Thanks
c96jgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

c96jgAuthor Commented:
Edited text of question.
0
ozoCommented:
#include <stdlib.h>
int atoi (const char *str);
0
jkrCommented:
ozo is right, but if you want enhanced error checking, use 'strtol()':

int n;
char* psz = "123";
char* pc;

n = ( int) strtol ( psz, &pc, 10);

if ( *pc)
{
 /* error */
}
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Ernest022699Commented:
Error checking is important; atoi() is not good enough unless you are certain that the string contains only digits (and possibly a sign character).  Ditto for strtol().  (If you use strtol(), be sure to have its second argument point to a valid terminating character or '\0'!)  Either of these library functions will return 0 (a VALID VALUE!) if they cannot convert the string to an integer!!!  Beware!

The careful programmer will loop through the string making sure there is nothing except leading whitespace (to be ignored), an optional sign (+ or -), and digits ('0' through '9').  For example:

int  iIndex;
int  iNumber=0;
int  iSign=0;  /* none seen yet */

char cTempChar;
char szMyString[MAX_STRING_LENGTH+1];

.... /* get the string, etc. */

for (iIndex=0;
     cTempChar=szMyString[iIndex];
     iIndex++)
{ /* loop through whole string */
/* check for digit
 | philosophy: most frequent case first
 */
if (isdigit(cTempChar) )
{
/* do the math:
 |  since we got a (or another) digit,
 |  multiply previous result by 10
 |  and add this digit;
 |  the character - the character zero
 |  == the numeric value
 */
iNumber = (10 * iNumber) +
          (cTempChar - '0');
/* ready for next character */
continue;
}

/* check for leading whitespace */
if (isspace(cTempChar) )
 {
 /* if we have seen any digits, error */
 if (iNumber)
  {
  /* printf error message */
  return (ERROR_CODE);
  /* or break, etc. */
  }
 continue;
 }

/* check for sign */
if ('+' == cTempChar)
 { /* positive */
 if (iSign)
  {/* error; we already saw a sign! */
  /* printf error message */
  return (ERROR_CODE);
  /* or break, etc. */
  }
 iSign = 1;
 continue;
 }

if ('-' == cTempChar)
 { /* negative */
 if (iSign)
  {/* error; we already saw a sign! */
  /* printf error message */
  return (ERROR_CODE); /* or break */
  }
 iSign = -1;
 continue;
 } /* end of negative */
} /* end of loop through whole string */

if (iSign)
 iNumber *= iSign;

/* use iNumber secure in the knowledge
 | that it is truly valid
 */
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
> Error checking is important;
Yes, it can be, but we weren't told what kinds of errors c96jg cares about or how they should be handled.
> Ditto for strtol().
And does your code check for any error conditions that jkr's code doesn't?

> /* use iNumber secure in the knowledge
> | that it is truly valid
>  */
Unless there was an overflow...
0
staierCommented:
try this
sscanf(buffer,"%i",&integervar)
0
c96jgAuthor Commented:
In this case error checking had been done thus I didn't say anything about it.

Thanks all anyway
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.