Thread: Converting string to integer...

  1. #1
    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
    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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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, 10:43 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03: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