Thread: string with 10 numbers to int, please. Can I avoid overflow?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    20

    string with 10 numbers to int, please. Can I avoid overflow?

    Hi,
    We are trying to convert a string that has an IP address that we have taken the dots out of. So the string is now
    "##########" (10 characters long where # = 0-9). we want to convert it to be some type of integer. we seem to run into overflow.
    we tried:

    longResult = atol(inputString.c_str());

    but we get overflow. we think that the atol function must use an integer data type at some point in the conversion causing overflow, maybe?

    If you have an idea we would love to hear about it.

    Thanks for listening and more thanks for an answer.

    Susan ( and son)

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    What type of integer? An average? Sum? Random?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    I will try and make it a little more clear, I hope.

    the number is in a string. it is 10 digits long. we want it to be the data type integer or long or what ever will hold it without going into E notation.

    long lgResult;
    string strInput = "1234567890";
    lgResult = strInput;

    I want the string to be converted to some type of integer that will not overflow.
    there is no math. it is just a data type conversion problem, i think.

    1234567890 = "1234567890"

    thanks,
    Susan (and son)

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Try using atof(). that will return a float

    An int is typically 16 or 32 bits
    that is a limit of 2^16 or 65535. A Long is 32 bits which is limited to 2^32or 4294967296.
    Keep in mind those are unsigned.
    Since that is an ip address I believe each field can go to 255 so the biggest number you will have is 255255255255. Much bigger than a long.
    MaxIP::::255255255255
    MaxLong:4294967296

    See its to small to hold it.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    I believe the e notation is only for display purposes.

    double d = atof("255255255255");
    cout << d << endl; //will show 2.55255e+011
    cout.precision(15);
    cout << d << endl; //will show 255255255255

    Hope that helps

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You were overflowing the int. You can use a double, but then you need to set the precision high enough so that it doesn't print in scientific notation.

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main(int argc, char *argv[]) {
        std::string inputAsString;
    
        std::cout << "Enter digits ";
        std::cin >> inputAsString;
    
        std::istringstream inputStream(inputAsString);
    
        double inputAsDouble;
    
        inputStream >> inputAsDouble;
    	
        std::cout.precision(15);
        std::cout <<  inputAsDouble << std::endl;
        
        return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Dah, go and test code and I am already too late .
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    double did it. thanks

    susan (and son)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM