operator>>
C++ でhex
値 AND と値の両方を読み取るように説得できdecimal
ますか? 次のプログラムは、16 進数の読み取りがどのようにうまくいかないかを示しています。と の両方を同じistringstream
ように読めるようにしたいです。hex
decimal
#include <iostream>
#include <sstream>
int main(int argc, char** argv)
{
int result = 0;
// std::istringstream is("5"); // this works
std::istringstream is("0x5"); // this fails
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;
}