DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
iostream examples

Conversion base

Integers are normally inserted and extracted in decimal notation, but this is controlled by flag bits. If none of ios::dec, ios::hex, or ios::oct are set the insertion is done in decimal but extractions are interpreted according to the C++ lexical conventions for integral constants. If ios::showbase is set then insertions will convert to an external form that can be read according to these conventions.

For example,

   int x = 64;
   cout << dec << x << " "
        << hex << x << " "
        << oct << x << endl ;
   cout.setf(ios::showbase,ios::showbase) ;
   cout << dec << x << " "
        << hex << x << " "
        << oct << x << endl ;
will result in the lines:
   64 40 100
   64 0x40 0100

setf() with only one argument turns the specified bits on, but doesn't turn any bits off.

Reading the lines shown above could be done by:

   cin >> dec >> x
       >> hex >> x
       >> oct >> x
       >> resetiosflags(ios::basefield)
       >> x >> x >> x ;
The value stored in x will be 64 for each extraction. The resetiosflags() manipulator turns off the specified bits in the flags.
Next topic: Miscellaneous formatting
Previous topic: Field widths

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004