iomanip

C

Christopher

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
 
J

Jon Bell

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

Jerry Coffin

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".
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top