Thread: Converting Strings of Numbers to Integers

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    23

    Converting Strings of Numbers to Integers

    How would I go about changing a string of numbers like "2546" and return the integer value 2546.

    This is what I have so far (this is just my function to convert the string to an integer)

    The 48 is in there because the value of 0 is 48, 1 is 49, etc

    I realize my "power" will depend on the length of the string of numbers but for now I have it set to 1 so it should work with any 2-digit integers.

    Code:
    int stringToInt ( char s[] )
    {
    	int i = 0;
    
    	int place;
    	int value = 0;
    	int power = 1;
    
    	for (i=0; s[i]!='\0' ; i++ )
    	{
    		place = s[i] - 48;
    		value = (value + (place*(10^power)));
    
    		power = power-1;
    
    	}
    
    	return place;
    }
    For some reason I'm only getting the last number as an integer from every string for instance, if the string is 48, I will get 8 returned (again, I'm just testing on 2-digit numbers right now)

    I will need this to work with any size string of numbers including negative numbers too.

    Any help would be great.

    Thanks

    Edit: I know I probably should have returned value at the end, but that's still wrong, it gives me a result of 61 when the string is 15, and 52 when the string is 23.
    Last edited by Adrian; 11-21-2007 at 09:50 PM.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Try atoi() or sscanf()

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Quote Originally Posted by cpjust View Post
    Try atoi() or sscanf()
    Sorry, but I don't know what either of those would do or where to implement them.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Uh huh, well I think the problem is actually your math. C doesn't have an exponent operator. operator ^ is bitwise exclusive OR, not the pow() function. I think you can do value *= 10 before you add the next place value to move along the conversion anyway so it shouldn't matter.

    consider something like this too.
    Code:
    int sign = 0;
    if ( i ==0 && s[i] == '-' ) {
       sign = 1;
       continue;
    }
    ...
    
    if( sign ) 
       value = -value;
    You can always just use the other functions that convert strings to numbers, like strtol(). it's useful to learn them.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Quote Originally Posted by citizen View Post
    Uh huh, well I think the problem is actually your math. C doesn't have an exponent operator. operator ^ is bitwise exclusive OR, not the pow() function. I think you can do value *= 10 before you add the next place value to move along the conversion anyway so it shouldn't matter.

    consider something like this too.
    Code:
    int sign = 0;
    if ( i ==0 && s[i] == '-' ) {
       sign = 1;
       continue;
    }
    ...
    
    if( sign ) 
       value = -value;
    You can always just use the other functions that convert strings to numbers, like strtol(). it's useful to learn them.
    Brilliant, thanks a lot, this works very well.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Actually, it gives me 0 for negative numbers because I'm returning 0 (since value = 0 before the for loop, which won't run if they have a '-' at the beginning of the string). How would I get the integer value for a negative number? I'm working on it now, but if you could help that would be awesome.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Somehow I have to ignore the '-' and do the same process but multiply it by -1 at the end. I think I can do this.

    Edit: I got it, thanks again for your help.
    Last edited by Adrian; 11-21-2007 at 10:32 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Nah. The point was to check the first character, and then wait until the end to negate the whole return value.

    1. Check for negative and set a flag
    2. Do the rest of the conversion work.
    3. If the flag is set, negate the value.
    4. return the value.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > value = (value + (place*(10^power)));
    Erm, ^ isn't "raise to power", it's exclusive-or.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cpjust View Post
    Try atoi() or sscanf()
    IIRC, atoi is not part of the standard, is it? It's better to use sprintf, or some function in that family.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    atoi is not part of the standard, is it?
    It is. itoa is the one that is not standard.
    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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Right, right, I confused those two.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting strings to chars
    By Suchy in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 04:17 AM
  2. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  3. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  4. convert numbers to strings
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2002, 03:43 PM