473,775 Members | 3,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9133
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********@gma il.com> wrote in message
news:11******** **************@ j73g2000cwa.goo glegroups.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::fmtfl ags f(std::cout.fla gs()); // 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::noupperca se' (the default).

-Mike
May 8 '06 #4

"Phlip" <ph******@yahoo .com> wrote in message
news:Ld******** ***********@new ssvr30.news.pro digy.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::fmtfl ags f(std::cout.fla gs()); // 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*******@gmai l.com> wrote in message
news:Kv******** ****@newssvr24. news.prodigy.ne t...
Mike Wahler wrote:
#include <ios>


Ja!
std::ios::fmtfl ags f(std::cout.fla gs()); // 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******@mkwah ler.net> wrote in message
news:Fd******** *******@newsrea d1.news.pas.ear thlink.net...

"Phlip" <ph*******@gmai l.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
14253
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
7991
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
1442
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
7600
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
1431
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
6080
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: ************ HelloWorld.cpp ************* #include "stdafx.h" void main()
10
3757
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. Now, if I use a href, the href will be executed as well. What can I du in order to make the href not execute if javascript is enabled?
16
2782
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
6866
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 something more subtle so that output of the following is also implementation-defined: if (INT_MAX == 65535) printf("%d", INT_MAX);
0
9458
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8943
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7465
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4023
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3615
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.