ascii to integer

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2007
Posts: 30
Reputation: RisTar is an unknown quantity at this point 
Solved Threads: 0
RisTar RisTar is offline Offline
Light Poster

ascii to integer

 
0
  #1
Feb 20th, 2007
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 ...
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: ascii to integer

 
0
  #2
Feb 20th, 2007
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:

  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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,500
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: ascii to integer

 
0
  #3
Feb 20th, 2007
To convert a single character to an int just simply subtract '0' from its ascii value
  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.
  1. num = (num * 10) + digits[i] - '0';
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 30
Reputation: RisTar is an unknown quantity at this point 
Solved Threads: 0
RisTar RisTar is offline Offline
Light Poster

Re: ascii to integer

 
0
  #4
Feb 22nd, 2007
Thanks guys , you were totally helpful .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC