any way to see the non-printable stuff in strings?

D

darren

Hi

I'm working on a project that has a lot of string processing going
on. I'm getting hung up because I can't see some of the hidden
characters in my strings (\n, \r\n, \t etc etc). Is there any way to
print out a string to standard out that shows these hidden characters
as escape sequences instead of their literal meaning?
 
S

sebastian

something like this would work:

std::string
expand_escapes( std::string const & text )
{
std::stringstream
result;
for
(
string::const_iterator
seq = text.begin( ),
fin = text.end( );
seq != fin;
seq++
)
{
if( isprint( *seq ) )
result << *seq;
else
result << "<" << ( int * )*seq << ">";
}
return result.str( );
}


int
main( void )
{
std::cout << expand_escapes( "bell: \b newline: \n crlf: \r\n" )
<< std::endl;
}
 
D

darren

something like this would work:

std::string
expand_escapes( std::string const & text )
{
std::stringstream
result;
for
(
string::const_iterator
seq = text.begin( ),
fin = text.end( );
seq != fin;
seq++
)
{
if( isprint( *seq ) )
result << *seq;
else
result << "<" << ( int * )*seq << ">";
}
return result.str( );

}

int
main( void )
{
std::cout << expand_escapes( "bell: \b newline: \n crlf: \r\n" )
<< std::endl;

}

cool, thanks for the help sebastian, i'll give it a try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top