 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mastupristi Guest
|
Posted: Thu Feb 24, 2005 8:04 am Post subject: 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?
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Thu Feb 24, 2005 9:15 am Post subject: Re: formatted output with cout |
|
|
Mastupristi wrote:
| Quote: | and obtain "0x015b", but I want uppercase characters.
|
I thought that
std::cout << std::hex << std::showbase << std::uppercase
<< std::internal << std::setw( << 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.
--
<http://www.contendix.com> - Software Development & Consulting
|
|
| Back to top |
|
 |
Mastupristi Guest
|
Posted: Thu Feb 24, 2005 10:26 am Post subject: Re: formatted output with cout |
|
|
On 24 Feb 2005 01:15:20 -0800
"Dietmar Kuehl" <dietmar_kuehl (AT) yahoo (DOT) com> wrote:
| Quote: | std::cout << std::hex << std::showbase << std::uppercase
std::internal << std::setw( << std::setfill('0')
0x015b << "n";
ok, now I obtain "0X015B" instead of "0x015B". |
How can be improved this print?
thanks
--
Mastupristi?
|
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Thu Feb 24, 2005 10:26 am Post subject: Re: formatted output with cout |
|
|
"Mastupristi" <cialdi_NO_SP (AT) AM_gmail (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Thu Feb 24, 2005 12:21 pm Post subject: Re: formatted output with cout |
|
|
"Mastupristi" <cialdi_NO_SP (AT) AM_gmail (DOT) com> wrote
| Quote: | On 24 Feb 2005 01:15:20 -0800
"Dietmar Kuehl" <dietmar_kuehl (AT) yahoo (DOT) com> wrote:
std::cout << std::hex << std::showbase << std::uppercase
std::internal << std::setw( << 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( << 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
|
|
| Back to top |
|
 |
Samuel Krempp Guest
|
Posted: Fri Mar 25, 2005 11:13 am Post subject: Re: formatted output with cout |
|
|
le Thursday 24 February 2005 13:21,
[email]NOT_VALID_please_use_contact_webform (AT) vecerina (DOT) com[/email] écrivit :
| Quote: | "Mastupristi" <cialdi_NO_SP (AT) AM_gmail (DOT) com> wrote in message
news:20050224112636.000010aa.cialdi_NO_SP (AT) AM_gmail (DOT) com...
On 24 Feb 2005 01:15:20 -0800
"Dietmar Kuehl" <dietmar_kuehl (AT) yahoo (DOT) com> wrote:
std::cout << std::hex << std::showbase << std::uppercase
std::internal << std::setw( << 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( << 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;
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|