468,110 Members | 1,827 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

printf("%x",...) in C <=> cout+ ? in C++

Dear all,

I have a question about using cout<<...

In standard C, I can use printf("%x",...) to print "hex" number. But in
Watcom C++, when using cout<<, I got the "decimal" representation !

Ex. a = 0x80000004
if ( using printf("%x",a) ) then I got "80000004" on screen

if ( using cout<<a ) then I got "2147483652"

Both results are correct but I want the former one...

Do i add some parameters together with cout to achieve my goal ?

Thanks !

May 8 '06 #1
9 8573
liaoo wrote:
Ex. a = 0x80000004
Not that 'a' is not "hex"; it's some bit pattern. Probably 1000...0100.
if ( using printf("%x",a) ) then I got "80000004" on screen

if ( using cout<<a ) then I got "2147483652"


Might not compile but enough to get you started:

#include <iomanip>

cout << "0x" << std::hex << a;

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 8 '06 #2
liaoo wrote:
I have a question about using cout<<...

In standard C, I can use printf("%x",...) to print "hex" number. But
in Watcom C++, when using cout<<, I got the "decimal" representation !

Ex. a = 0x80000004
if ( using printf("%x",a) ) then I got "80000004" on screen

if ( using cout<<a ) then I got "2147483652"

Both results are correct but I want the former one...

Do i add some parameters together with cout to achieve my goal ?


You add <<hex between 'cout' and '<<a'. And read about IO manipulators.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 8 '06 #3

"liaoo" <ji********@gmail.com> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...
Dear all,

I have a question about using cout<<...

In standard C, I can use printf("%x",...) to print "hex" number. But in
Watcom C++, when using cout<<, I got the "decimal" representation !

Ex. a = 0x80000004
if ( using printf("%x",a) ) then I got "80000004" on screen

if ( using cout<<a ) then I got "2147483652"

Both results are correct but I want the former one...

Do i add some parameters together with cout to achieve my goal ?

Thanks !


#include <ios>
#include <iostream>

int main()
{
std::ios::fmtflags f(std::cout.flags()); // save prev format flags
std::cout << std::hex << 42 << '\n'; // output in hex
std::cout.flags(f); // restore prev format flags
return 0;
}

Output:

2a

If you want upper case for digits A-F, insert the manipulator
'std::uppercase' before 'std::hex'. Lower case is specified
by 'std::nouppercase' (the default).

-Mike
May 8 '06 #4

"Phlip" <ph******@yahoo.com> wrote in message
news:Ld*******************@newssvr30.news.prodigy. com...
liaoo wrote:
Ex. a = 0x80000004
Not that 'a' is not "hex"; it's some bit pattern. Probably 1000...0100.
if ( using printf("%x",a) ) then I got "80000004" on screen

if ( using cout<<a ) then I got "2147483652"


Might not compile but enough to get you started:

#include <iomanip>


Make that
#include <ios>

<iomanip> declares manipulators which take arguments,
e.g. 'setw()', 'setprecision()', etc.


cout << "0x" << std::hex << a;


-Mike
May 8 '06 #5
Mike Wahler wrote:
#include <ios>
Ja!
std::ios::fmtflags f(std::cout.flags()); // save prev format flags
std::cout << std::hex << 42 << '\n'; // output in hex
std::cout.flags(f); // restore prev format flags


Not that we all don't enjoy a little overkill, but...

....doesn't the stream reset its own flags at flush time or something?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 8 '06 #6

"Phlip" <ph*******@gmail.com> wrote in message
news:Kv************@newssvr24.news.prodigy.net...
Mike Wahler wrote:
#include <ios>


Ja!
std::ios::fmtflags f(std::cout.flags()); // save prev format flags
std::cout << std::hex << 42 << '\n'; // output in hex
std::cout.flags(f); // restore prev format flags


Not that we all don't enjoy a little overkill, but...

...doesn't the stream reset its own flags at flush time or something?


No. There's one exception where a flag does not
retain its state during the lifetime of a stream
object: setting the field width with 'setw()'.
It always gets reset to 'setw(0)' (width is
equal to width of the data) for subsequent
insertions. I sometimes find that inconvenient,
but there it is.

-Mike
May 8 '06 #7

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Fd***************@newsread1.news.pas.earthlin k.net...

"Phlip" <ph*******@gmail.com> wrote in message

...doesn't the stream reset its own flags at flush time or something?
No. There's one exception where a flag does not
retain its state during the lifetime of a stream
object: setting the field width with 'setw()'.
It always gets reset to 'setw(0)' (width is

equal to width of the data) for subsequent ^
formatted insertions.


-Mike
May 8 '06 #8
Mike Wahler wrote:
...doesn't the stream reset its own flags at flush time or something?


No. There's one exception...


Speaking of exceptions. ;-)

What happens if your 42 threw an exception?

(Discuss literal integers can't throw exceptions here ->[ ]. Y'all know what
I mean...)

Are there any cute systems to make the IO state exception-safe?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 9 '06 #9
On Tue, 09 May 2006 00:51:37 +0000, Phlip wrote:
Mike Wahler wrote:
...doesn't the stream reset its own flags at flush time or something?

No. There's one exception...


Speaking of exceptions. ;-)

What happens if your 42 threw an exception?

(Discuss literal integers can't throw exceptions here ->[ ]. Y'all know what
I mean...)

Are there any cute systems to make the IO state exception-safe?


Something something RAII something. Create a class whose constructor
accepts a stream reference and saves the stream's state and whose
destructor restores the state of the stream. Make it uncopyable (private
copy c'tor and assignment operator), for good measure. Create one on the
stack (IosGard foo (cout); cout << hex << ...) prior to changing the
stream.
May 9 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Gernot Frisch | last post: by
8 posts views Thread by Ben | last post: by
5 posts views Thread by Paminu | last post: by
9 posts views Thread by Richard Lionheart | last post: by
10 posts views Thread by Gernot Frisch | last post: by
16 posts views Thread by Gernot Frisch | last post: by
26 posts views Thread by Yevgen Muntyan | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.