This is the problem, you need to pull out all the values ​​from the line containing something like "\ x1 \ x2 \ xd \ x3" and recalculate their bytes.

std::string s= "\x1\x2\xd\x3" const char* y = s.c_str(); ???? 
  • Well, I translate for the rest "polish" - this is Ukrainian. polichiti - recalculate. But what do you mean by "pull out all the values"? - Harry
  • @Harry: Is it possible to "list"? - VladD
  • @VladD Well, in Lingvo, this is still the second meaning of the word :) - Harry
  • @Harry, I would suggest that it was a typo, and there should have been a word "get." Perhaps it was necessary to get characters from a string, and for each character to get the bytes representing it - Grundy
  • one
    What does it mean to count their bytes ? Suppose you can calculate the amount as follows: int sum = 0; for (int i = 0; i < s.length(); i++) sum += (unsigned char)s[i]; cout << sum << '\n'; int sum = 0; for (int i = 0; i < s.length(); i++) sum += (unsigned char)s[i]; cout << sum << '\n'; , and you can still a bunch of ways, but I think the idea of ​​access to each char inside the string you caught. - avp

1 answer 1

This is what you need? :)

 void outbytes(const std::string& s) { std::cout << std::setfill('0') << std::hex; for(unsigned char c: s) std::cout << "\\x" << std::setw(2) << static_cast<int>(с) << " "; std::cout << std::endl; } 
  • Only c to unsigned char give, if you want all bytes only 2 hexes to be output. - avp
  • @avp - I will not tell you about the standard, but Visual C ++ will start displaying it with characters. But when casting to int - everything is in order. - Harry
  • one
    Coercion to int for an output as number, of course, all the same is necessary. You now have bytes with a negative value (for example, the letter 'I' in the cp1251 encoding) will be displayed as \ xffffffNN (8 digits). In short, it is necessary like this: `... << (int) (unsigned char) c <<" ";` - avp
  • one
    Well, or auto replace unsigned char . - StateItPrimitive
  • @avp that for signed char , that for unsigned char will display a character . If you need a code - you have to lead to numeric types . - αλεχολυτ