Strtok and replace substring in C ++
3 answers
In doubt, it depends on what you really want:
if you want the result to be the same: char s [] = {0x12, 0x34, 0x56, 0x78, '\ 0'}: then you can do this:
std::string s;
int val;
std::stringstream ss("12 34 56 78");
while(ss >> std::hex >> val) {
s += static_cast<char>(val);
}
after that you can check it like this:
for(int i = 0; i < s.length(); ++i) {
printf("%02x\n", s[i] & 0xff);
}
which will print:
12 34 56 78
otherwise, if you wanted your string to literally be "\ x12 \ x23 \ x34 \ x56", you could do what Jesse Beder suggested.
+2
source to share
You can do it like this:
foreach( character in source string)
{
if
character is ' ', write ' \x' to destination string
else
write character to destination string.
}
I suggest using std :: string, but it can be done easily by first checking the string to count the number of spaces and then creating a new destination string as number x3.
0
source to share