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

Discussion in 'C++' started by Steven Woody, Jul 10, 2008.

  1. Steven Woody

    Steven Woody Guest

    Here is the code,

    uint16_t n1, n2;
    ...
    std::eek: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
     
    Steven Woody, Jul 10, 2008
    #1
    1. Advertising

  2. Steven Woody a écrit :
    > Here is the code,
    >
    > uint16_t n1, n2;
    > ...
    > std::eek: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
     
    Michael DOUBEZ, Jul 10, 2008
    #2
    1. Advertising

  3. Steven Woody

    Steven Woody Guest

    On Jul 10, 7:14 pm, Michael DOUBEZ <> wrote:
    > Steven Woody a écrit :
    >
    > > Here is the code,

    >
    > > uint16_t n1, n2;
    > > ...
    > > std::eek: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.
     
    Steven Woody, Jul 11, 2008
    #3
  4. Steven Woody

    James Kanze Guest

    On Jul 10, 12:47 pm, Steven Woody <> wrote:
    > Here is the code,


    > uint16_t n1, n2;
    > ...
    > std::eek: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:
    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
     
    James Kanze, Jul 11, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,177
    Janaka
    Feb 18, 2004
  2. beginner

    Awkward format string

    beginner, Aug 1, 2007, in forum: Python
    Replies:
    11
    Views:
    496
    beginner
    Aug 2, 2007
  3. Ben Phillips
    Replies:
    6
    Views:
    874
    Roger Lindsjö
    Aug 23, 2008
  4. rickman

    Awkward Arithmetic

    rickman, Mar 15, 2010, in forum: VHDL
    Replies:
    9
    Views:
    594
    rickman
    Mar 22, 2010
  5. Joe Snodgrass

    Compilation of Awkward Syntax

    Joe Snodgrass, Apr 27, 2013, in forum: C++
    Replies:
    25
    Views:
    486
    Stuart
    May 7, 2013
Loading...

Share This Page