Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can I format stream not in such an awkward way?

Reply
Thread Tools

How can I format stream not in such an awkward way?

 
 
Steven Woody
Guest
Posts: n/a
 
      07-10-2008
Here is the code,

uint16_t n1, n2;
...
std:stringstream os;
os << "(" << std::hex << std::setw(4) << std::setfill('0')
<< n1 << "," << std::setw(4) << std::setfill('0')
<< n2 << ")";

You see, I have to used setw,setfill twice for print two integer.
This is the only we I found works. Is there a simple representaion to
archive same purpose?

Thanks!

-
narke
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      07-10-2008
Steven Woody a écrit :
> Here is the code,
>
> uint16_t n1, n2;
> ...
> std:stringstream os;
> os << "(" << std::hex << std::setw(4) << std::setfill('0')
> << n1 << "," << std::setw(4) << std::setfill('0')
> << n2 << ")";
>
> You see, I have to used setw,setfill twice for print two integer.
> This is the only we I found works. Is there a simple representaion to
> archive same purpose?


You don't need the second std::setfill('0') but you need a setw() for
each output concerned.
If you do:
os<<std::setw(4) << std::setfill('0')<<",";
output is '000,'.
os.setf(ios::hex, ios::basefield)
os.fill('0');
os << "("
<< std::setw(4) << n1 << ","
<< std::setw(4) << n2
<< ")";

--
Michael
 
Reply With Quote
 
 
 
 
Steven Woody
Guest
Posts: n/a
 
      07-11-2008
On Jul 10, 7:14 pm, Michael DOUBEZ <(E-Mail Removed)> wrote:
> Steven Woody a écrit :
>
> > Here is the code,

>
> > uint16_t n1, n2;
> > ...
> > std:stringstream os;
> > os << "(" << std::hex << std::setw(4) << std::setfill('0')
> > << n1 << "," << std::setw(4) << std::setfill('0')
> > << n2 << ")";

>
> > You see, I have to used setw,setfill twice for print two integer.
> > This is the only we I found works. Is there a simple representaion to
> > archive same purpose?

>
> You don't need the second std::setfill('0') but you need a setw() for
> each output concerned.
> If you do:
> os<<std::setw(4) << std::setfill('0')<<",";
> output is '000,'.
> os.setf(ios::hex, ios::basefield)
> os.fill('0');
> os << "("
> << std::setw(4) << n1 << ","
> << std::setw(4) << n2
> << ")";
>
> --
> Michael


Thank you, I understand.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-11-2008
On Jul 10, 12:47 pm, Steven Woody <(E-Mail Removed)> wrote:
> Here is the code,


> uint16_t n1, n2;
> ...
> std:stringstream os;
> os << "(" << std::hex << std::setw(4) << std::setfill('0')
> << n1 << "," << std::setw(4) << std::setfill('0')
> << n2 << ")";


> You see, I have to used setw,setfill twice for print two
> integer. This is the only we I found works. Is there a
> simple representaion to archive same purpose?


os << '(' << HexFmt( 4 ) << n1 << ',' << HexFmt( 4 ) << n2 <<
')' ;

More generally, the standard manipulators (except maybe for
setw) are really only there to serve as examples. You wouldn't
normally use them in real code; you'd define application
specific manipulators, like HexFmt above. (My implementation of
HexFmt is available at my site, but more generally, you'll want
to provide your own, since only you know what logical markup is
applicable to your application.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Awkward format string beginner Python 11 08-02-2007 02:55 PM
Hardware Motherboard Awkward Problem at Startup - VIA K7VM2 QUIROGACA Computer Support 2 11-14-2006 06:49 AM
A little awkward Anonyma Computer Support 9 06-27-2006 06:33 AM
The Kanye West show was Mike Myers SECOND awkward live tv moment heinzmenses@hotmail.com Digital Photography 31 09-12-2005 08:54 PM



Advertisments