944,047 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 16202
  • C++ RSS
Feb 20th, 2007
0

ascii to integer

Expand Post »
hi , im pretty new to computer science and alose to this community . i have a problem that im trying to solve and hope you guys can help me with it . im trying to create my own version of the function "atoi" that can be found in stdlib and i cant get it to work . i know how to convert a single char from the string to and int , but its starting to be a problem when im trying to convert a number like "111" , if any1 can help it could be great . thanks ...
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
RisTar is offline Offline
51 posts
since Feb 2007
Feb 20th, 2007
0

Re: ascii to integer

I assume you have this one function which can convert a single character to a number. Say it is int convert_one_char(const char c).

Given this function here is the code to extend it to convert a string with multiple chars:

c++ Syntax (Toggle Plain Text)
  1. int convert_one_char(const char c)
  2. {
  3. if( c == '0' ) return 0;
  4. else if( c == '1' ) return 1;
  5. else if( c == '2' ) return 2;
  6. else if( c == '3' ) return 3;
  7. else if( c == '4' ) return 4;
  8. else if( c == '5' ) return 5;
  9. else if( c == '6' ) return 6;
  10. else if( c == '7' ) return 7;
  11. else if( c == '8' ) return 8;
  12. else if( c == '9' ) return 9;
  13. else return -1 ;
  14. }
  15.  
  16. long my_atoi( const string& s )
  17. {
  18. int curr_dec_multiplier = 10 ;
  19. long ret_number = convert_one_char(s[s.length() - 1]) ;
  20. for( int i = s.length() - 2; i >= 0; i-- )
  21. {
  22. ret_number += convert_one_char(s[i]) * curr_dec_multiplier ;
  23. curr_dec_multiplier *= 10 ;
  24. }
  25.  
  26. return ret_number ;
  27. }
  28.  
  29. //------------------------------------------------
  30.  
  31. int main ()
  32. {
  33. cout << "523623412 converted = " << my_atoi("523623412") << endl ;
  34. cout << "623412 converted = " << my_atoi("623412") << endl ;
  35. cout << "231 converted = " << my_atoi("231") << endl ;
  36.  
  37. //-----------------------
  38. getch() ;
  39. return 0;
  40. }
  41. //------------------------------------------------


You'll need to put in appropriate error handling code...
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Feb 20th, 2007
0

Re: ascii to integer

To convert a single character to an int just simply subtract '0' from its ascii value
C++ Syntax (Toggle Plain Text)
  1. char c = '1';
  2. int num = c - '0';

When converting several characters to int you have to multiply the previous value by 10 to make room for the new digit.
Below num is an integer and digits is a character array that contains all the digits. This code snipped would be placed inside a loop.
C++ Syntax (Toggle Plain Text)
  1. num = (num * 10) + digits[i] - '0';
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Feb 22nd, 2007
0

Re: ascii to integer

Thanks guys , you were totally helpful .
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
RisTar is offline Offline
51 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: E-mail using C
Next Thread in C++ Forum Timeline: urgent!!! problem in using fstream.h





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC