iomanip

Discussion in 'C++' started by Christopher, Nov 9, 2003.

  1. Christopher

    Christopher Guest

    Can someone give me an example of how to use the hex manipulator defined in
    <iomanip>. I am reading the doc but it doesnt give me the syntax, or at
    least I don't understand it.
    Thanx,
    Christopher
    Christopher, Nov 9, 2003
    #1
    1. Advertising

  2. Christopher

    Jon Bell Guest

    In article <VPxrb.23106$>,
    Christopher <> wrote:
    >Can someone give me an example of how to use the hex manipulator defined in
    ><iomanip>.


    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main ()
    {
    int num = 1234;
    cout << "Decimal = " << num
    << ", Hexadecimal = " << hex << num << endl;
    return 0;
    }


    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Nov 9, 2003
    #2
    1. Advertising

  3. Christopher

    Jerry Coffin Guest

    In article <VPxrb.23106$>,
    says...
    > Can someone give me an example of how to use the hex manipulator defined in
    > <iomanip>. I am reading the doc but it doesnt give me the syntax, or at
    > least I don't understand it.


    #include <iostream>
    #include <iomanip>

    int main() {

    std::cout << std::hex << 12345;
    return 0;
    }

    Note that there are two entirely different things named "hex" -- the one
    I used above is a manipulator. The other (std::ios::hex) is a constant
    value that will set a base to hexadecimal when it's passed to
    setiosflags -- if you accidentally print it out, you'll get some value
    (normally a power of two) preceding your own value, which will still be
    printed in decimal. E.g. the output from the program above should be
    "3039", which is 12345 converted to hexadecimal. OTOH, code like this:
    std::cout << std::ios::hex << 12345;
    will typically produce output something like "1612345" or "204812345".

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Nov 9, 2003
    #3
    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. Vaxius
    Replies:
    3
    Views:
    3,438
  2. alternativa
    Replies:
    4
    Views:
    588
    Alan Woodland
    May 17, 2006
  3. rupert

    <iomanip> precision value

    rupert, Jul 29, 2006, in forum: C++
    Replies:
    2
    Views:
    2,669
    Mike Wahler
    Jul 29, 2006
  4. Replies:
    3
    Views:
    488
    Victor Bazarov
    Sep 3, 2007
  5. brekehan

    iomanip - alignment

    brekehan, Oct 18, 2007, in forum: C++
    Replies:
    2
    Views:
    593
    James Kanze
    Oct 19, 2007
Loading...

Share This Page