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();
}
}
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
You know bitwise operators?
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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?
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Yeah, that part I get.
But do you want the 31 bits as a number (like 7274749912) or as bits (like 11100011100101010010)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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();
}
}
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
These are the first 31 bits. The first bit is the one on the right (also known as lsb)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403