My understanding is that reading a uint8_t
from a stringstream
is a problem because the stringstream
will interpret the uint8_t
as a char
. I would like to know how I can read a uint8_t
from a stringstream
as a numeric type. For instance, the following code:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
uint8_t ui;
std::stringstream ss("46");
ss >> ui;
cout << unsigned(ui);
return 0;
}
prints out 52
. I would like it to print out 46
.
EDIT: An alternative would to just read a string
from the stringstream
and then convert the solution to uint8_t
, but this breaks the nice chaining properties. For example, in the actual code I have to write, I often need something like this:
void foobar(std::istream & istream){
uint8_t a,b,c;
istream >> a >> b >> c;
// TODO...
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…