473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 3969
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 beginner of C/C++ programming. I don't know how to...
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 the input to throw an ios_base::failure, the...
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 without modification to the base code. As a...
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 this performance should it become an issue? ...
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 via vb.net code. The formatting contains the...
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; //generate adress in hex format...
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 '1' rather than 0x31; If I use int cast, it can...
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 %6d"); How can I do such a thing with C++...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.