There is a hexstr_to_str function that reads from memory (not a file, not cin) a text stream of the form "01 03 10 fa", and writing to std :: string containing \ x01 \ x03 \ x10 \ xfa. The problem with reading integers from a stream is that after reading the last number , the flag is set in the failbit stream. Is there an alternative to istringstream, or after reading each number from the stream, it is necessary to do a check at the end of the string, for example, through peek ()? Is there a simpler solution?

#include <iostream> #include <sstream> ssize_t hexstr_to_str(std::string &destination, std::istringstream &stream_source) { std::ios::fmtflags old_flags = stream_source.setf(std::ios::hex, std::ios::basefield); size_t count = 0; int symbol = -1; destination.clear(); while ( stream_source >> symbol ) //добавление && !stream_source.eof() результата не дало. { destination += symbol; ++count; } stream_source.setf(old_flags); std::cout << (stream_source.rdstate() & (stream_source.failbit | stream_source.badbit))<<"\n"; return !(stream_source.rdstate() & (stream_source.failbit | stream_source.badbit)) ? count : -count; } int main() { std::string hb_str; //std::cout << hexstr_to_str(hello_str, std::istringstream(cstr_hex_hello)); hexstr_to_str(hb_str, std::istringstream("10 20 af 30")); return 0; } 
  • one
    Why do we need streams at all if the buffer in the memory of your format is easily disassembled via access to this memory itself via str [i]? - Vladimir Martyanov
  • @ Vladimir Martiyanov buffer is stored in text format. string a = "10 20 af 30"; str [0] = 1 in this case. I need 0x10. Or did you mean something else? - borat_brata
  • stream_source >> std :: hex should help - Chorkov
  • @Chorkov alas, but all the same. - borat_brata
  • @borat_brata is what I meant. stream'y not needed, even no library functions are not needed to solve the problem. - Vladimir Martyanov

2 answers 2

Why failbit you think the presence of a failbit is a problem?

Forget about it, this is normal behavior at the end of the line.

Check eof - if the end of the line is reached, then the parsing of the line was successful. If not achieved, then there was an error:

 return stream_source.eof() ? count : -count; 
  • I consider a problem from the documentation. Displayed flag means bad. But perhaps you are right eof helps. - borat_brata
  • @borat_brata, crosses is a crazy language in general - avp
  • @avp, at first glance, I liked the pure C more, but I try to study them in parallel. In the pluses code to write faster somehow. - borat_brata
  • @borat_brata, imho You are absolutely right, and since many people write in C ++, you will still need knowledge in it. - avp

Just in case (amused)

 using namespace std; string get_xstr (string str, long *errpos) { if (errpos) *errpos = -1; istringstream istr(str); ostringstream ostr; long x, curpos; istr.setf(std::ios::hex, std::ios::basefield); ostr.setf(std::ios::hex, std::ios::basefield); while (istr >> x) { curpos = istr.tellg(); ostr << "\\x" << x; } if (!istr.eof() && errpos) *errpos = curpos; return ostr.str(); } 

Note that the possible position of an error in a string has to be taken in a loop, since the behavior of .tellg() similar to delirium with failbit .