Converting string to integer...

This is a discussion on Converting string to integer... within the C++ Programming forums, part of the General Programming Boards category; Say I have a string (str) that contains a very long signed integer, like +1234567890 or -1234567890. How can I ...

  1. #1
    Ham
    Ham is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    14

    Converting string to integer...

    Say I have a string (str) that contains a very long signed integer, like +1234567890 or -1234567890. How can I convert this string to an integer value that will preserve all 10 values (including the sign would be nice too)? I've tried functions like atol but it doesn't work. A small code example should suffice.

    Thanks.

  2. #2
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    This seemed to work perfectly fine for me:

    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <stdio.h>
    
    int main() 
    {
    	char strNumber[20] = "-3003820";
    	long number;
    	//strNumber[0] = '0';
    	number = strtol( strNumber, '\0', 10 );
    
    	printf("String was: %s, number is: %i", strNumber, number );
    
    	return 0;
    }
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Search the board. Not too long ago there was a thread full of information on this topic. I believe it was called 'atoi??' or something like that. Always search the board before posting a question.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Ham
    Ham is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    Nice try, but that didn't work for numbers like +9999999999. Searching the board didn't really help. Any takers?

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Here's my stab at it:

    Code:
    long bigatoi (const char* n)
    {
    	int neg = 0;
    	if (n[0] == '-') 
    		neg = 1;
    	
    	int l = strlen(n);
    	int i = neg;
    	long c = pow(10,(l-1-neg));
    	long num = 0;
    
    	for (; i < l; ++i)
    	{
    		num += ((int)(n[i]-48) * c);
    		c /= 10;
    	}
    
    	if (neg)
    		return -num;
    
    	return num;
    }
    There's plenty of ways that code could be better, I'm sure.

    The problem is that really big numbers are require more space. You can replace the long c =, long num = and return type with long doubles, which have more space (tried it but numbers come back in sci. notation)...

    I'll try to improve this code...
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    I dunno how to get it to work with bigger numbers =)

    Another way would be to create (or use a preexisting, perhaps) "big number" class, that can store and work with numbers with very long digit counts.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Code:
    int
     as_int(const char c)
    {
     return c - 0x30;
    }
    
    
    template <class number>
    number
     power(number value, int exp)
    {
     if(exp < 1) return 1;
     number result = value;
        while(--exp)
      {
        result *= value;
      }
     return result;
    }
    
    
    template <class number>
    number
     aton(const char * input)
    {
     int x = 0;
     number value = 0;
     char * p = (char*)&input[ strlen(input) ];
      while(p != &input[0])
     {
        --p;
    
          if(isdigit(*p))
        {
          value += ( as_int(*p) * power<number>(10, x++) );
        }
         else if(*p == '-')
        {
          value = -value;
        }
      }
     return value;
    }
    
    
    
    
    
    int main(){
    char buff[100];
    
       do
      {
        cout << "...enter a number, '0' to exit..." << endl;
        cin.getline(buff, 100);
        cout << aton<long long int>(buff) << endl;
      }while(aton<long long int>(buff) != 0);
    
     cin.get();
    return 0;
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Converting a string to an integer
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2001, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21