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

L

liaoo

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 !
 
P

Phlip

liaoo said:
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;
 
V

Victor Bazarov

liaoo said:
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
 
M

Mike Wahler

liaoo said:
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
 
M

Mike Wahler

Phlip said:
Not that 'a' is not "hex"; it's some bit pattern. Probably 1000...0100.


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
 
P

Phlip

Mike said:
#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?
 
M

Mike Wahler

Phlip said:
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
 
P

Phlip

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?
 
O

Owen Jacobson

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.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top