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

iomanip

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <VP******************@twister.austin.rr.com>,
Christopher <cp***@austin.rr.com> 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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #2

P: n/a
In article <VP******************@twister.austin.rr.com>,
cp***@austin.rr.com 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.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.