What is the C++
equivalent of the following Java
line of code
int x = Integer.parseInt("0010011110", 2);
std::stoi (since C++11):
int x = std::stoi("0010011110", nullptr, 2);
You can use strtol
to parse an integer in base 2:
const char *binStr = "0010011110";
char *endPtr;
int x = strtol(binStr, &endPtr, 2);
cout << x << endl; // prints 158
Here is a link to a demo on ideone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With