By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,108 Members | 1,398 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,108 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Jul 23 '05 #2

P: n/a
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 );

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.