# converting char to int

• 11-27-2008
IM back!
converting char to int
hello

I want to convert a char array containing only numbers like char aa[50]="123456789" into a int with the value 123456789.

i thought that i could to it by simply going: number in place* 10^place, and reapeating that for every place: like so 9+8*10^2=89 , 89+7*10^2=789, 789+6*10^3=6789 etc

i coded the following to do this:

Code:

```int char_to_int ( char IT[100] ) {         cout<<"----debug int char_to_int ( char IT[100] )----\n";         cout<<"IT = "<<IT<<endl;         int result=0;         int bb = 0;         int aa = char_length ( IT );         cout<<"char_length ( IT ) = "<< char_length ( IT ) <<endl;         while ( aa > 0 )         {                 if ( bb == 0 )                 {                         cout<<"IT[aa] = "<<IT[aa]<<endl;                         cout<<"IT[aa]-48 = "<<IT[aa] - 48<<endl;                         result = result + ( IT[aa] - 48 );                         cout<<"result = "<<result<<endl;                         aa--;                         bb++;                 }                 else                 {                         cout<<"IT[aa] = "<<IT[aa]<<endl;                         cout<<"IT[aa]-48 = "<<IT[aa] - 48<<endl;                         result = result + ( IT[aa] - 48 ) * 10^bb;                         cout<<"result = "<<result<<endl;                         aa--;                         bb++;                 }         }         cout<<"----debug int char_to_int ( char IT[100] ) END----\n";         return result; }```
but it dosent work (surprised:rolleyes:)

this is the output:

Code:

```----debug int char_to_int ( char IT[100] )---- IT = 112233 char_length ( IT ) = 6 IT[aa] = IT[aa]-48 = -48 result = -48 IT[aa] = 3 IT[aa]-48 = 3 result = -17 IT[aa] = 3 IT[aa]-48 = 3 result = 15 IT[aa] = 2 IT[aa]-48 = 2 result = 32 IT[aa] = 2 IT[aa]-48 = 2 result = 48 IT[aa] = 1 IT[aa]-48 = 1 result = 63 ----debug int char_to_int ( char IT[100] ) END----```
look at the out put see this line IT[aa] = why isent there anything printed there? :confused:

EDIT: i know this method wont work if the input includes a 0
• 11-27-2008
matsp
In C, x ^ y doesn't do x to the power of y. You can do that with the cmath function pow(), but for converting digits, it's actually easier to just start from the left, grab one digit, multiply the result so far by 10, and add the current digit. Keep going until there is no more digits.

--
Mats
• 11-27-2008
laserlight
It would be easier to use a stringstream, e.g.,
Code:

```char aa[50] = "123456789"; std::stringstream ss(aa); int num; ss >> num;```
• 11-27-2008
matsp
Quote:

Originally Posted by laserlight
It would be easier to use a stringstream, e.g.,
Code:

```char aa[50] = "123456789"; std::stringstream ss(aa); int num; ss >> num;```

Of course! However, I got the impression that the original post was some sort of asignment or such, and that such a solution is "cheating". I could be wrong.

--
Mats
• 11-27-2008
Elysia
Or my favorite, boost::lexical_cast:
Code:

`int x = boost::lexical_cast<int>("12345678");`
That is, if you are allowed to use these shortcuts, and it is not an assignment.
It's part of the Boost library (boost.org).
• 11-27-2008
CornedBee
atoi is about four lines of code. matsp described the algorithm.