Is it possible to convince the operator → in C ++ to read both the hexadecimal value of AND and the decimal value? The following program demonstrates how reading hexadecimal code does not work correctly. I would like the same istringstream to be able to read both hexadecimal and decimal numbers.
#include <iostream>
#include <sstream>
int main(int argc, char** argv)
{
int result = 0;
std::istringstream is("0x5");
while ( is.good() ) {
if ( is.peek() != EOF )
is >> result;
else
break;
}
if ( is.fail() )
std::cout << "failed to read string" << std::endl;
else
std::cout << "successfully read string" << std::endl;
std::cout << "result: " << result << std::endl;
}
mes5k source
share