473,396 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 9100
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: cylin | last post by:
Dear all, To do below is too long. cout << "Name: " << name << ", Age: " << age << endl; Can cout use "%" to implement? Thanks. cylin.
14
by: Gernot Frisch | last post by:
Hi, I want to represent a double in char* as accurate as possible, but as short as possible: double char* 10000 1E5 1000.00123 1000.00123 ....
8
by: Ben | last post by:
(I am using gcc 3.2 on RH 8) int name (struct str *name) I call the above function like this: struct str buf; int conf = name(&buf); int j; for (j=0; buf.address; j++) {
8
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
5
by: Paminu | last post by:
I thought that "->" was only used with pointers. Why is it that it is also allowed to use it on arrays?? struct test { int a; int b; }; struct test array;
9
by: Richard Lionheart | last post by:
Hi All, I've got Visual Studio .Net installed, but I don't know it very well. So I tried to create a plain old Win32 using the command-line complier. I tried to compile: ************...
10
by: Gernot Frisch | last post by:
Hi, I'm currently writing: <span onclick="window.open(...);">Klick Here</span> but I want to use the <a href> for this, since it is defined in the css script the way I want my link to open....
16
by: Gernot Frisch | last post by:
Hi, class MyString { char* m_data; public: MyString(const char* c) { m_data = new char; strcpy(m_data, c);
26
by: Yevgen Muntyan | last post by:
Hey, It was mentioned elsewhere that printf("%d", INT_MAX); is implementation-defined. Why? Is it because INT_MAX value is implementation-defined so output depends on implementation, or is it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.