formatted output with cout

M

Mastupristi

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
 
D

Dietmar Kuehl

Mastupristi said:
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.
 
M

Mastupristi

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
 
J

John Carson

Mastupristi said:
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

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;
 
I

Ivan Vecerina

Mastupristi said:
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...
 
S

Samuel Krempp

le Thursday 24 February 2005 13:21,
(e-mail address removed) écrivit :
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.)
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top