there is a line in the memory of the form "90 14 55 af 9b ... etc." how to set the read flags from the string of 16 values ​​correctly?

#include <iostream> #include <sstream> std::istringstream ss ("10 03 02 01 ff 01 00 00 d8 03 02 01 "); int main() { int x = -1; //ss.setf(std::ios::hex); спотыкается на "ff", не читает в дальнейшем в 16-ном режиме, такое ощущение, что флаг не устанавливается //ss>>std::hex; в данном случае всё отлично работает while (ss >> x) { std::cout<<x<<" "; } return 0; } 
  • What do you want to bring to the console in the end? Line "10 03 02 01 ff 01 00 00 d8 03 02 01 " ? The character of each byte? Or like this \x10 \x03 \x02 \x01 \xff \x01 \x00 \x00 \xd8 \x03 \x02 \x01" ? - Duracell
  • @Duracell "\ x10 \ x03 \ x02 \ x01 \ xff \ x01 \ x00 \ x00 \ xd8 \ x03 \ x02 \ x01". The console is still for clarity, and save the result in std :: string, but this is not the problem. Interests how correctly setf use, but not ss >> std::hex; - borat_brata

2 answers 2

Corrected:

 #include <iostream> #include <sstream> std::istringstream ss ("10 03 02 01 ff 01 00 00 d8 03 02 01 "); int main() { int x = -1; while (ss >> std::hex >> x) { std::cout << std::dec << x << " "; } return 0; } 

When you put the std::hex or std::dec manipulator, they are set to flow until they are switched.

Option with setf :

 #include <iostream> #include <sstream> std::istringstream ss ("10 03 02 01 ff 01 00 00 d8 03 02 01 "); int main() { int x = -1; ss.setf(std::ios::hex, std::ios::basefield); while (ss >> x) { std::cout << x << " "; } return 0; } 
  • Thanks, but I'm interested in how to use setf. - borat_brata
  • Added option with setf - user1056837
  • It is clear what the error was. It was necessary to specify the basefield. Thank. - borat_brata

In general, here are two options for you, choose which one you need:

1.In decimal form:

 #include <iostream> #include <sstream> std::istringstream ss("10 03 02 01 ff 01 00 00 d8 03 02 01 "); int main() { int x = -1; //ss.setf(std::ios::hex); спотыкается на "ff", не читает в дальнейшем в 16-ном режиме, такое ощущение, что флаг не устанавливается //ss>>std::hex; в данном случае всё отлично работает while (ss >> std::hex >> x) { std::cout << x << " "; } getchar(); return 0; } 

The result will be:

16 3 2 1 255 1 0 0 216 3 2 1

  1. In hexadecimal form:

#include <iostream>

#include <sstream>

 std::istringstream ss("10 03 02 01 ff 01 00 00 d8 03 02 01 "); int main() { int x = -1; //ss.setf(std::ios::hex); спотыкается на "ff", не читает в дальнейшем в 16-ном режиме, такое ощущение, что флаг не устанавливается //ss>>std::hex; в данном случае всё отлично работает while (ss >> std::hex >> x) { std::cout << std::hex << x << " "; } getchar(); return 0; } 

The answer will be:

10 3 2 1 ff 1 0 0 d8 3 2 1

  • And now the same thing, but without ss >> std::hex >> x , and using ss.setf (AND THAT'S HERE SHOULD BE HERE AND IT IS POSSIBLE AT ALL). - borat_brata