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

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

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;
}
 
U

upashu2

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
}

No, It is right effect.After calling hex manipulator,all output should
be in hexadecimal format, until u reset it.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top