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:
but it dosent work (surprisedCode: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; })
this is the output:
look at the out put see this line IT[aa] = why isent there anything printed there?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----
thanks for your help.
EDIT: i know this method wont work if the input includes a 0



LinkBack URL
About LinkBacks
)




