472,328 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a...
4
by: rossum | last post by:
I have been looking at exceptions as I need to get better at using them. I came across an interesting effect, demonstrated below. When I ctrl-Z...
4
by: Joe C | last post by:
I've written a console application and would like to isolate all screen output so that it will be easier to migrate the code to a GUI-type platform...
6
by: Jason Heyes | last post by:
I am starting to worry about the performance of formatted read/write operations on data-redundant objects in my program.What can I do to improve...
2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
3
by: Craig Petrie | last post by:
Hi, I have a large table in Word 2003 that has formatted text in the cells and wish to read and convert a cells formatted contents to html output...
4
by: mwebel | last post by:
Hi, i want to get the adress of a pointer in a string. I did so by outputting it to a ostringstream: ------------------------------ char* ptr;...
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a...
6
by: Jojo | last post by:
Hi all, I was wondering how I can perform formatted output with C++ strings. For example, suppose I have in plain C: sprintf(C_string, "%5.2f ...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.