Reputation: 59
I have string which contains unprintable/control characters -
\222!,\306G6qh\341Pp\210;\241\2619}\222+"\340\315\364ƅ\344\264\215\230\3130ZG!\214\344y\307W(\254Y^\213F\234oz\263V^\274\2453 L
The value of this string comes from a function and is different every time the program is run.
Is there any way I can printf
/cout
this string ?
Upvotes: 0
Views: 2452
Reputation: 27577
Simply print them as hex, something like this [with EDIT from feedback]:
#include <ctype.h>
for( char c : str )
if (isprint(c))
if (c = '\\')
std::cout << "\\\\";
else
std::cout << c;
else
std::cout << "\0x" << std::hex
<< static_cast<int>(static_cast<unsigned char>(c));
Upvotes: 2
Reputation: 385284
Loop through the string and, for each character, if it's within the range of what you consider "printable", print it. Otherwise, output some alternative representation of your choice; perhaps a backslash followed by a three-digit octal number representing the character's value in integer form.
Upvotes: 0