473,499 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3973
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
872
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
1956
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
3554
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
2185
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
3492
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
3352
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
1639
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
33268
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
3667
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++...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.