473,471 Members | 4,625 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I format stream not in such an awkward way?

Here is the code,

uint16_t n1, n2;
...
std::ostringstream os;
os << "(" << std::hex << std::setw(4) << std::setfill('0')
<< n1 << "," << std::setw(4) << std::setfill('0')
<< n2 << ")";

You see, I have to used setw,setfill twice for print two integer.
This is the only we I found works. Is there a simple representaion to
archive same purpose?

Thanks!

-
narke
Jul 10 '08 #1
2 1513
On Jul 10, 7:14 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
Steven Woody a écrit :
Here is the code,
uint16_t n1, n2;
...
std::ostringstream os;
os << "(" << std::hex << std::setw(4) << std::setfill('0')
<< n1 << "," << std::setw(4) << std::setfill('0')
<< n2 << ")";
You see, I have to used setw,setfill twice for print two integer.
This is the only we I found works. Is there a simple representaion to
archive same purpose?

You don't need the second std::setfill('0') but you need a setw() for
each output concerned.
If you do:
os<<std::setw(4) << std::setfill('0')<<",";
output is '000,'.
os.setf(ios::hex, ios::basefield)
os.fill('0');
os << "("
<< std::setw(4) << n1 << ","
<< std::setw(4) << n2
<< ")";

--
Michael
Thank you, I understand.
Jul 11 '08 #2
On Jul 10, 12:47 pm, Steven Woody <narkewo...@gmail.comwrote:
Here is the code,
uint16_t n1, n2;
...
std::ostringstream os;
os << "(" << std::hex << std::setw(4) << std::setfill('0')
<< n1 << "," << std::setw(4) << std::setfill('0')
<< n2 << ")";
You see, I have to used setw,setfill twice for print two
integer. This is the only we I found works. Is there a
simple representaion to archive same purpose?
os << '(' << HexFmt( 4 ) << n1 << ',' << HexFmt( 4 ) << n2 <<
')' ;

More generally, the standard manipulators (except maybe for
setw) are really only there to serve as examples. You wouldn't
normally use them in real code; you'd define application
specific manipulators, like HexFmt above. (My implementation of
HexFmt is available at my site, but more generally, you'll want
to provide your own, since only you know what logical markup is
applicable to your application.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Trevor | last post by:
Hello, Please bear with me, I am trying to learn C++. I am implementing some error/debug functions which format a message and output it to a C++ stream. I would like to design it using one low...
5
by: Andrew Slentz | last post by:
I have a format file which is working but not correctly. It is, for some reason, dropping the first line of the input .csv file. The problem is something with the second coulumn of data having...
0
by: Franky | last post by:
Hello, I have some application mesages in plain text (with some legacy protocol) that needs to be converted to XML format and then send the converted XML messages to another host. Such messages...
5
by: Lee Gillie | last post by:
I am using Cryptography. You can encrypt or decrypt by providing an output stream as a parameter to the CryptoStream constructor. But I need byte arrays, as I am encrypting on the fly to a socket,...
2
by: Pat | last post by:
Here is my problem that I am having with a current project. I went and backed up all my hard drives to DVD's. After backing up the drives, I used a batch file to get all the file names from the...
4
by: MC | last post by:
In C, printf allows us to specify a runtime (non-constant) field width of a formatted value by using the * character. Is there something like that for System.String.Format? In C#, I find myself...
3
by: Matt | last post by:
Does the standard define default values for the format state of an ostream? I would like to be able to put a stream in a standard format state without thinking about how some other routine may...
4
by: Fresh_Air_Rider | last post by:
Hi In the "good old" Classic ASP days, I used to stream records from a SQL Server database out to the user's browser in CSV format by using a combination of COALESCE and the ADODB.Stream object....
5
by: veaux | last post by:
I'm thinking this is easy but can't get it. I have a table with following: Table1 Date 1/1/2007 Table2 Type 0107 (This is MMYY of above) So I'm having trouble using a query to turn the...
4
by: john | last post by:
I am reading TC++PL3, and on page 468, at "21.4.3 Floating-Point Output", it formats floating point output in the style: cout.setf(ios_base::scientific, ios_base::floatfield); // use scientific...
0
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.