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

• 01-02-2002
Susan
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)
• 01-02-2002
CodeMonkey
What type of integer? An average? Sum? Random?
• 01-02-2002
Susan
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)
• 01-02-2002
RpiMatty
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.
• 01-02-2002
RpiMatty
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
• 01-02-2002
SilentStrike
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; }```
• 01-02-2002
SilentStrike
Dah, go and test code and I am already too late ;).
• 01-02-2002
Susan
double did it. thanks

susan (and son)