Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Converting chars to int

Posted on 1997-11-19
4
Medium Priority
?
307 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 100 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

596 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