"<< hex" vs cout.setf(ios_base::hex)

Discussion in 'C++' started by Generic Usenet Account, Jul 11, 2005.

  1. What exactly is the difference between the hex manipulator and the
    following statement: cout.setf(ios_base::hex)?

    According to Stroustrup, Third Edition, Section 21.4.4, "once set, a
    base is used until reset". For some reason, I interpreted this to
    mean that "<< hex" manipulator would cause the base to be set for only
    the current statement, while the cout.setf(ios_base::hex) statement
    would cause the base to be set for multiple statements, up to the point
    where it is reset. The following snippet of code, which dumps an
    arbitrary data structure, seems to behave EXACTLY THE OPPOSITE WAY.

    Thanks,
    Gus


    ///////////////////////////////////////

    #include <iostream>
    #include <sys/types.h>

    using namespace std;

    #define DS_DUMP_LEN 10


    struct point
    {
    int x;
    int y;
    };

    struct rect
    {
    point p1;
    point p2;
    };



    void
    dumpMemory(void* ds, size_t size)
    {
    // Uncomment the next line for the desired behavior
    // (i.e. restricting the hexadecimal base just to this function)
    // ios_base::fmtflags oldFmtSettings = cout.setf(ios::showbase);
    char *dsPtr = (char *)ds;
    for(size_t i = 0; i < size; i++)
    {
    if(!(i%DS_DUMP_LEN))
    cout << endl;
    unsigned char ch = *(dsPtr+i);
    cout << hex << int(ch) << " ";
    }
    cout << endl;

    // Uncomment the next line for the desired behavior
    // (i.e. restricting the hexadecimal base just to this function)
    //cout.setf(oldFmtSettings);
    }

    ////////////////////////////////////////////////////////////
    // The intent is to have a hex base only in the dumpMemory()
    // routine, and a decimal base everywhere else.
    ////////////////////////////////////////////////////////////
    main()
    {
    int temp = 16;
    rect myRect = {{3, 5}, {14, 47}};

    // The following line seems to have no "lingering" effect
    // The output that follows still has a decimal base
    cout.setf(ios_base::hex);
    cout << "Value @ location 1 is " << temp << endl;

    dumpMemory(&myRect, sizeof(rect));

    // We used the hex manipulator only in the dumpMemory function,
    // but it seems to have left behind a lingering effect, and
    // the output that follows continues to have a hexadecimal base
    cout << "Value @ location 2 is " << temp << endl;
    }
    Generic Usenet Account, Jul 11, 2005
    #1
    1. Advertising

  2. Generic Usenet Account

    upashu2 Guest

    It doesn't related with function call. It is related with setf() and
    hex manipulator. Following example shows same thing,
    int main( )
    {
    using namespace std;
    int i = 16,j=24;
    cout << i << endl; ///prints 16

    cout.setf( ios_base::hex );
    cout << i << endl; ///still prints 16
    cout<<j<<endl; ///pritns 24
    cout<<hex<<i<<endl; ///prints 10
    cout<<j<<endl; ////prints 18
    }

    then why setf() is not working? since we haven't reset ios_base::dec,
    by calling unsetf(). Now modify the code, all things will work as you
    want.
    int main( )
    {
    using namespace std;
    int i = 16,j=24;
    cout << i << endl; ///prints 16
    cout.unsetf( ios_base::dec ); ///<B> See This</B>
    cout.setf( ios_base::hex );
    cout << i << endl; ///still prints 10
    cout<<j<<endl; ///pritns 18
    //cout<<hex<<i<<endl; ///prints 10
    // cout<<j<<endl; ////prints 18
    }

    >>but it seems to have left behind a lingering effect,.
    >>the output that follows continues to have a hexadecimal base


    No, It is right effect.After calling hex manipulator,all output should
    be in hexadecimal format, until u reset it.
    upashu2, Jul 12, 2005
    #2
    1. Advertising

  3. Generic Usenet Account

    upashu2 Guest

    these two lines
    >>cout.unsetf( ios_base::dec ); ///<B> See This</B>
    >>cout.setf( ios_base::hex );

    can be replaced with
    cout.setf( ios_base::hex, ios_base::dec );
    upashu2, Jul 12, 2005
    #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. Geoffrey S. Knauth
    Replies:
    6
    Views:
    998
    Earl Purple
    Jan 18, 2006
  2. DaVinci
    Replies:
    3
    Views:
    424
    Jacek Dziedzic
    Apr 9, 2006
  3. DaVinci
    Replies:
    7
    Views:
    717
    Fraser Ross
    Apr 7, 2006
  4. Robin
    Replies:
    9
    Views:
    1,251
  5. jdm
    Replies:
    3
    Views:
    1,346
    James Kanze
    Nov 9, 2010
Loading...

Share This Page