469,645 Members | 1,752 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

formatted output with cout

I want to obtain the c++ equivalent of:

unsigned short us = 347;
printf("0x%04hX",us);

that outputs "0x015B"

I ried with:

cout.setf(ios_base::hex,ios_base::basefield);
cout.setf(ios_base::showbase);
cout.width(6);
cout.fill('0');
cout << 347 << endl;

But I obtain "00x15b"
Then I tried to modify:

cout.setf(ios_base::hex,ios_base::basefield);
cout << "0x";
cout.width(4);
cout.fill('0');
cout << 347 << endl;

and obtain "0x015b", but I want uppercase characters.

How can I obtain what I want in a simple way using cout?

thanks

--
Mastupristi?
Jul 23 '05 #1
5 3725
Mastupristi wrote:
and obtain "0x015b", but I want uppercase characters.


I thought that

std::cout << std::hex << std::showbase << std::uppercase
<< std::internal << std::setw(8) << std::sefill('0')
<< 0x015b << "\n";

should do the trick but at least on the machine I'm at it does
not. On another machine it does, however.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #2
On 24 Feb 2005 01:15:20 -0800
"Dietmar Kuehl" <di***********@yahoo.com> wrote:
std::cout << std::hex << std::showbase << std::uppercase
<< std::internal << std::setw(8) << std::setfill('0')
<< 0x015b << "\n";

ok, now I obtain "0X015B" instead of "0x015B".
How can be improved this print?

thanks

--
Mastupristi?
Jul 23 '05 #3
"Mastupristi" <cialdi_NO_SP@AM_gmail.com> wrote in message
news:20050224090430.00007f43.cialdi_NO_SP@AM_gmail .com
I want to obtain the c++ equivalent of:

unsigned short us = 347;
printf("0x%04hX",us);

that outputs "0x015B"

I ried with:

cout.setf(ios_base::hex,ios_base::basefield);
cout.setf(ios_base::showbase);
cout.width(6);
cout.fill('0');
cout << 347 << endl;

But I obtain "00x15b"
Then I tried to modify:

cout.setf(ios_base::hex,ios_base::basefield);
cout << "0x";
cout.width(4);
cout.fill('0');
cout << 347 << endl;

and obtain "0x015b", but I want uppercase characters.

How can I obtain what I want in a simple way using cout?

thanks

--
Mastupristi?


Streams are a mystery to me, but the following seems to work:

cout.setf(ios_base::hex, ios_base::basefield);
cout.setf(ios::uppercase);
cout << "0x";
cout.width(4);
cout.fill('0');
cout << 347 << endl;
--
John Carson
Jul 23 '05 #4
"Mastupristi" <cialdi_NO_SP@AM_gmail.com> wrote in message
news:20050224112636.000010aa.cialdi_NO_SP@AM_gmail .com...
On 24 Feb 2005 01:15:20 -0800
"Dietmar Kuehl" <di***********@yahoo.com> wrote:
std::cout << std::hex << std::showbase << std::uppercase
<< std::internal << std::setw(8) << std::setfill('0')
<< 0x015b << "\n";

ok, now I obtain "0X015B" instead of "0x015B".
How can be improved this print?


I guess you could try:
std::cout << "0x" << std::hex << std::uppercase
<< std::internal << std::setw(8) << std::setfill('0')
<< 0x015b << "\n";

Now if you ask me, I wouldn't bother wrestling with the
standard C++ streams formatting. I'd go for boost::format
or some other kind of wrapper around the C library calls...

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #5
le Thursday 24 February 2005 13:21,
NO**********************************@vecerina.com écrivit :
"Mastupristi" <cialdi_NO_SP@AM_gmail.com> wrote in message
news:20050224112636.000010aa.cialdi_NO_SP@AM_gmail .com...
On 24 Feb 2005 01:15:20 -0800
"Dietmar Kuehl" <di***********@yahoo.com> wrote:
std::cout << std::hex << std::showbase << std::uppercase
<< std::internal << std::setw(8) << std::setfill('0')
<< 0x015b << "\n";

ok, now I obtain "0X015B" instead of "0x015B".
How can be improved this print?


I guess you could try:
std::cout << "0x" << std::hex << std::uppercase
<< std::internal << std::setw(8) << std::setfill('0')
<< 0x015b << "\n";

Now if you ask me, I wouldn't bother wrestling with the
standard C++ streams formatting. I'd go for boost::format
or some other kind of wrapper around the C library calls...


boost::format is not a wrapper around C lib calls, it's a wrapper around
stream calls.. so it'll actually do the same thing as the above lines.
(but it would be possible to overload boost::format for basic types and
forward those to a suitable function of the printf family. It might help
performance.. and ease compatibility with printf for basic types :) I
should try that when I have some time.)

--
Samuel.Krempp
cout << "@" << "crans." << (is_spam ? "trucs.en.trop." : "" )
<< "ens-cachan.fr" << endl;

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Ram Laxman | last post: by
4 posts views Thread by rossum | last post: by
4 posts views Thread by Joe C | last post: by
2 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by Craig Petrie | last post: by
4 posts views Thread by mwebel | last post: by
6 posts views Thread by Jojo | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.