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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Woody

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

    Here is the code,

    uint16_t n1, n2;
    ...
    std::ostringstr eam 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

    #2
    Re: How can I format stream not in such an awkward way?

    On Jul 10, 7:14 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
    Steven Woody a écrit :
    >
    Here is the code,
    >
    uint16_t n1, n2;
    ...
    std::ostringstr eam 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::he x, ios::basefield)
    os.fill('0');
    os << "("
    << std::setw(4) << n1 << ","
    << std::setw(4) << n2
    << ")";
    >
    --
    Michael
    Thank you, I understand.

    Comment

    • James Kanze

      #3
      Re: How can I format stream not in such an awkward way?

      On Jul 10, 12:47 pm, Steven Woody <narkewo...@gma il.comwrote:
      Here is the code,
      uint16_t n1, n2;
      ...
      std::ostringstr eam 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:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      Working...