Solved

Converting chars to int

Posted on 1997-11-19
4
242 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
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
int atoi (const char *str);

0
 
LVL 22

Accepted Solution

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

Expert Comment

by:ozo
Comment Utility
or
      i += *Str++ - '0';

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now