Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #5146
| From | "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Printing non-printable characters |
| Date | 2011-05-18 23:36 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <ir1dqa$5uj$1@dont-email.me> (permalink) |
| References | <49349a99-b157-4b5e-ac1f-570c8f06fc98@s14g2000vbi.googlegroups.com> |
* Alex Vinokur, on 18.05.2011 09:55:
> Hi,
>
> // --------------------
> #include<iostream>
> #include<iomanip>
Add relevant header for 'isprint'.
> int main()
> {
> char data[5];
> data[0] = 'a';
> data[1] = 5;
> data[2] = 'b';
> data[3] = 10;
> data[4] = 0;
char const data[] = "a\005b\012";
Or thereabouts. It's a long time since I used that silly octal notation. :-)
> for (std::size_t i = 0; i< sizeof(data); i++)
for( int i = 0; data[i] != 0; ++i )
> {
> char ch = data[i];
char const ch = data[i];
>
> if (isprint(static_cast<int>(ch)) != 0)
if( isprint( static_cast<unsigned char>( ch ) ) != 0 )
While the earlier things are mostly style, the type of this cast is important to
avoid Undefined Behavior. If you can guaranteee ASCII only data then it does not
matter, but then you don't need any cast. The correct type is 'unsigned char'
(with a subsequent implicit conversion to 'int').
> {
> std::cout<< ch;
> }
> else
> {
> std::cout<< "\\"<< std::oct<< static_cast<int>(ch)<< std::dec;
> }
{
std::cout << ch;
}
else
{
std::cout << "\\" << std::oct << ch+0 << std::dec;
}
Please convert tab characters to spaces before posting.
> }
> std::cout<< std::endl;
>
> return 0;
> }
>
> // ------------------
>
> Output:
> a\5b\12\0
>
> Is it possible to get the same or similar output without loop in the
> program?
There will be a loop somewhere.
The only question is how indirect and hidden you want it.
In general it's better with explicit code than indirect, hidden, implicit code.
Cheers & hth.,
- Alf
--
blog at <url: http://alfps.wordpress.com>
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Printing non-printable characters Alex Vinokur <alex.vinokur@gmail.com> - 2011-05-18 00:55 -0700
Re: Printing non-printable characters Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-05-18 03:20 -0700
Re: Printing non-printable characters Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-05-18 08:00 -0700
Re: Printing non-printable characters Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-18 09:05 -0400
Re: Printing non-printable characters Michael Doubez <michael.doubez@free.fr> - 2011-05-18 06:59 -0700
Re: Printing non-printable characters Michael Doubez <michael.doubez@free.fr> - 2011-05-18 08:02 -0700
Re: Printing non-printable characters Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-05-18 21:06 +0000
Re: Printing non-printable characters "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-18 23:36 +0200
Re: Printing non-printable characters Leigh Johnston <leigh@i42.co.uk> - 2011-05-18 23:32 +0100
csiph-web