Hi,
I have a following problem. I have a number in string, for example "123456" and I'd like to convert it to int. This would not be a problem if I knew that the input number will always fit into an 32 bit integer. So if it won't I should take only the first 31 bit of that number, show some error and the program should continue. I know that there is the atoi function which I think can handle the overflow but I need to write my own function and I don't know how.

Thx for any help.

Recommended Answers

All 15 Replies

You should use the atoi function in C++. Conversion are done easier with the use of a stringstream. By checking the stream after the conversion, we can see if the conversion has succeeded.

#include <sstream>
#include <string>
#include <iostream>
#include <exception>

int strToInt(std::string in) {
    int ret_val = 0;
    std::stringstream sstr(in);
    sstr >> ret_val;
    if (sstr.fail()) { //Check if conversion worked
        throw std::exception(std::string("Failed conversion for: " + in).c_str());
    }
    return ret_val;
}

int main() {

    try {
        int i = strToInt("1000000"); // Will fit
        int j = strToInt("20000000000"); // Wont fit
    } catch (std::exception &ex) {
        std::cout << "ERROR: " << ex.what();
    }
}

You should use the atoi function in C++. Conversion are done easier with the use of a stringstream. By checking the stream after the conversion, we can see if the conversion has succeeded.

#include <sstream>
#include <string>
#include <iostream>
#include <exception>

int strToInt(std::string in) {
    int ret_val = 0;
    std::stringstream sstr(in);
    sstr >> ret_val;
    if (sstr.fail()) { //Check if conversion worked
        throw std::exception(std::string("Failed conversion for: " + in).c_str());
    }
    return ret_val;
}

int main() {

    try {
        int i = strToInt("1000000"); // Will fit
        int j = strToInt("20000000000"); // Wont fit
    } catch (std::exception &ex) {
        std::cout << "ERROR: " << ex.what();
    }
}

Thx for the reply, this really works but if it won't fit I need the first 31 bits of that number and the rest throw away. Any idea?

You know bitwise operators?

You know bitwise operators?

Yes I know them but still I don't know how it has to be done

I'm not going to give it away. :)
Let's start with a smaller number, like 7.

which number and which operator do you need to get the last bit (=1 because 7 = 0111 binary).

0111
xxxx
----- ??? operator
0001

Choose from or, and, xor.

I'm not going to give it away. :)
Let's start with a smaller number, like 7.

which number and which operator do you need to get the last bit (=1 because 7 = 0111 binary).

0111
xxxx
----- ??? operator
0001

Choose from or, and, xor.

Ok 1001 and

Actually 0001 and. You put a 1 at each position you want the bit from. So in your case you need the last 31 bits which is 01111111111111111111111111111111 which is 2147483647 decimal. So if you do int last31 = youinput & 2147483647; you should be done.

Actually 0001 and. You put a 1 at each position you want the bit from. So in your case you need the last 31 bits which is 01111111111111111111111111111111 which is 2147483647 decimal. So if you do int last31 = youinput & 2147483647; you should be done.

Thanks again,I get it but I still don't know how to get the bit array of the number which is stored in that string.

I don't understand. Do you want
1. The last 31 bits as a decimal number?
2. The last 31 bits as an array (bitset) of 1's and 0's?

I don't understand. Do you want
1. The last 31 bits as a decimal number?
2. The last 31 bits as an array (bitset) of 1's and 0's?

What I exactly need is: I will get an string which contains a really large number like "123456789123456789" which don't fit into an integer and I need the first 31 bits of this number(not string).

Yeah, that part I get.
But do you want the 31 bits as a number (like 7274749912) or as bits (like 11100011100101010010)

Yeah, that part I get.
But do you want the 31 bits as a number (like 7274749912) or as bits (like 11100011100101010010)

If I will have the decimal or binary I can simply convert it to another, so it doesn't matter I just need to know how to get that (especially in c++).

In that case I already told you the answer.

Integrated in the program:

#include <sstream>
#include <string>
#include <iostream>
#include <exception>

int strToInt(std::string in) {
    unsigned long long ret_val = 0;
    std::stringstream sstr(in);
    sstr >> ret_val;
    if (sstr.fail()) { //Check if conversion worked
        throw std::exception(std::string("Failed conversion for: " + in).c_str());
    }
    return static_cast<int>(ret_val & 2147483647); //use bitwise & with the last 31 bits.
}

int main() {

    try {
        std::cout << strToInt("1234567890") << '\n';// show entire number, because it fits an int
        std::cout << strToInt("12345678900")<< '\n';// Only last 31 bits because number is too big.
    } catch (std::exception &ex) {
        std::cout << "ERROR: " << ex.what();
    }
}

In that case I already told you the answer.

Integrated in the program:

#include <sstream>
#include <string>
#include <iostream>
#include <exception>

int strToInt(std::string in) {
    unsigned long long ret_val = 0;
    std::stringstream sstr(in);
    sstr >> ret_val;
    if (sstr.fail()) { //Check if conversion worked
        throw std::exception(std::string("Failed conversion for: " + in).c_str());
    }
    return static_cast<int>(ret_val & 2147483647); //use bitwise & with the last 31 bits.
}

int main() {

    try {
        std::cout << strToInt("1234567890") << '\n';// show entire number, because it fits an int
        std::cout << strToInt("12345678900")<< '\n';// Only last 31 bits because number is too big.
    } catch (std::exception &ex) {
        std::cout << "ERROR: " << ex.what();
    }
}

Alright this works good but I have two more question. Why does it fail on number bigger than 123456789012345678901 (10 times bigger). And I wanted the first 31 bits not the last.

These are the first 31 bits. The first bit is the one on the right (also known as lsb)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.