Solved

Converting chars to int

Posted on 1997-11-19
4
292 Views
Last Modified: 2010-04-10
Do you know the algorithm for converting a string of digits, character by character, to a decimal integer?  For example,
if the user entered a string of the characters '1' '2' '3',
how would you convert those digits to another variable of type int with the value 123?
If you know, please email me at:  jose_gutierrez@usa.net
Thanks.
0
Comment
Question by:Jose_G
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1173601
int atoi (const char *str);

0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1173602
int
CnvStr2Int(char *Str)
{
   int i = 0;
   while (*Str)
   {
      i *= 10;
      i += *Str++;
   }
   return i;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 1173603
or
      i += *Str++ - '0';

0
 
LVL 22

Expert Comment

by:nietod
ID: 1173604
whoops.  Yes, you do want to subtract off 0.

int
CnvStr2Int(char *Str)
{
   int i = 0;
   while (*Str)
   {
      i *= 10;
      i += *Str++ - '0';
   }
   return i;
}
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question