std::ios::hex

L

Luther Baker

Hi,

This doesn't do what I expect.

int t = 27;
std::cout << "myHex: '" << std::ios::hex << t << "'" << std::endl;
std::cout.setf(std::ios::hex);
std::cout << "moreHex: '" << t << "'" << std::endl;

VC++ 7.1 says:

myHex: '204827'
moreHex: '27'

while gcc 3.3 says:

myHex: '827'
moreHex: '27'

Instead of converting the stream to hex, it appears I am just printing a
platform specific constant in front of my variable.

Simple words of wisdom to print hex?

Thanks,

-Luther
 
S

SaltPeter

Luther Baker said:
Hi,

This doesn't do what I expect.

int t = 27;
std::cout << "myHex: '" << std::ios::hex << t << "'" << std::endl;
std::cout.setf(std::ios::hex);
std::cout << "moreHex: '" << t << "'" << std::endl;

VC++ 7.1 says:

myHex: '204827'
moreHex: '27'

while gcc 3.3 says:

myHex: '827'
moreHex: '27'

Instead of converting the stream to hex, it appears I am just printing a
platform specific constant in front of my variable.

Simple words of wisdom to print hex?

Thanks,

-Luther

Try....

#include <iostream>

int main()
{
int t = 27;
std::cout << "myHex: '" << std::hex << t << "'" << std::endl;
t = 127;
std::cout << "moreHex: '" << t << "'" << std::endl;
t = 99;
std::cout << "dec: '" << std::dec << t << "'" << std::endl;

return 0;
}
 
I

Ian

Luther said:
Hi,

This doesn't do what I expect.

int t = 27;
std::cout << "myHex: '" << std::ios::hex << t << "'" << std::endl;
std::cout.setf(std::ios::hex);
std::cout << "moreHex: '" << t << "'" << std::endl;

VC++ 7.1 says:

myHex: '204827'
moreHex: '27'

while gcc 3.3 says:

myHex: '827'
moreHex: '27'

Instead of converting the stream to hex, it appears I am just printing a
platform specific constant in front of my variable.

Simple words of wisdom to print hex?
std::ios::hex is a bitmask, 8 on gcc and 2048 on VC. That's why it
works with setf().

std::hex is the operator.

Ian
 
L

Luther Baker

SaltPeter said:
Try....

#include <iostream>

int main()
{
int t = 27;
std::cout << "myHex: '" << std::hex << t << "'" << std::endl;
t = 127;
std::cout << "moreHex: '" << t << "'" << std::endl;
t = 99;
std::cout << "dec: '" << std::dec << t << "'" << std::endl;

return 0;
}

Thanks much. I'm still not sure how to use cout.setf(...) correctly,
but this suggestion will get me by.

-Luther
 
S

SaltPeter

Luther Baker said:
Ian <[email protected]> wrote in message

Ok, I see. Poor Josuttis.


But it *doesn't* work with setf.

Thanks,

-Luther

Sure it does:

#include <iostream>

int main()
{
int n = 255;
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout << "n in hex is " << n << std::endl;
return 0;
}

The std::ios::basefield flag toggles the other bits pertaining to oct and
dec. Not using basefield in this case and not using this version of setf()
is therefore undefined. Thats what the std::hex() manipulator in last
message i sent does for you.
 
L

Luther Baker

SaltPeter wrote:
....
Sure it does:

#include <iostream>

int main()
{
int n = 255;
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout << "n in hex is " << n << std::endl;
return 0;
}

The std::ios::basefield flag toggles the other bits pertaining to oct and
dec. Not using basefield in this case and not using this version of setf()
is therefore undefined. Thats what the std::hex() manipulator in last
message i sent does for you.

Ah, I see how it works now. Like posix signal handlers ...

Thanks for staying with me there.

-Luther
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top