Thread: converting char to int

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    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)

    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?

    thanks for your help.

    EDIT: i know this method wont work if the input includes a 0
    Last edited by IM back!; 11-27-2008 at 02:59 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be easier to use a stringstream, e.g.,
    Code:
    char aa[50] = "123456789";
    std::stringstream ss(aa);
    int num;
    ss >> num;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    atoi is about four lines of code. matsp described the algorithm.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM