473,768 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"<< hex" vs cout.setf(ios_b ase::hex)

What exactly is the difference between the hex manipulator and the
following statement: cout.setf(ios_b ase::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_b ase::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::fmtfl ags 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(oldFm tSettings);
}

////////////////////////////////////////////////////////////
// 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_b ase::hex);
cout << "Value @ location 1 is " << temp << endl;

dumpMemory(&myR ect, 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
2 8865
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<<e ndl; ///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<<e ndl; ///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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
1501
by: qazmlp | last post by:
I hope comp.lang.c will not find the following question as a complete off-topic. I would like to remove ie.comment out the 'cout' statements during compilation(actually preprocessing) time. The statements like this: cout<<"something\n" ; should be made as
6
21067
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use. What the code does: * First writes "This is sent to prompt" to prompt.
5
4006
by: Mastupristi | last post by:
I want to obtain the c++ equivalent of: unsigned short us = 347; printf("0x%04hX",us); that outputs "0x015B" I ried with: cout.setf(ios_base::hex,ios_base::basefield);
10
3508
by: Gurikar | last post by:
How to make cout not printing on the console. i mean void main() { cout<<"Hello world"<<endl; } Is there any way where i can block printing on console even when iam
7
7573
by: Joe C | last post by:
I'd like to have better control of text output to the console. I'm using Windows and have a little (non-standard) function to help me out: #include <windows.h> void locate(int x, int y ) { COORD cur = { x, y }; SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur ); }
16
3993
by: mrDumbass | last post by:
#include <iostream> #include <algorithm> #include <string> #include <fstream> using namespace std; /* i would like to keep the numbers of the output : Power , step etc, on the same place in the console. But this isnt going to work with
58
4850
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better" style and what the pros and cons are (I'm using cout as example, but it really applies to any similar construct): 1) using std::cout;
1
1269
by: William Chavula | last post by:
void basic() { cout<<"---FOLLOW THESE STEPS TO DIAGNOSE INTERNET PROBLEMS---\n\n"; cout<<"---STEP 1---\n\n"; cout<<"Run your mobile partner software\n\n"; cout<<"---STEP 2---\n\n"; cout<<"Click on TOOLS in the menu bar\n\n"; cout<<"---STEP 3---\n\n"; cout<<"In the tools menu click on PROFILE MANAGEMENT\n\n";
0
9577
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9412
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10185
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9969
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8843
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6659
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3938
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3543
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.