473,666 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
+ 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::ostringstr eam 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 1523
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::ostringstr eam 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::he x, 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...@gma il.comwrote:
Here is the code,
uint16_t n1, n2;
...
std::ostringstr eam 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 objektorientier ter Datenverarbeitu ng
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
7751
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 level function which accepts a "generic C++ stream" as an argument (cerr, cout, ofstream, etc). I have tried to make the parameter a const ostream&, but when I plug in cerr or cout I get a compiler error. What am I doing wrong? // The low...
5
1496
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 quotes in it. Any ideas? Below is some info. Format file (I use firstrow=2 in Bulk Import command: 8.0 6 1 SQLCHAR 0 3000 ",\"" 1 Provider_Raw_ID Latin1_General_CI_AS 2 SQLCHAR 0 3000 "\"," 0 none_name Latin1_General_CI_AS
0
927
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 are like a few thousand and could use different schemas (like a few hundreds of schema files). Is the DataSet the best way to format the XML stream? For each schema, a DataSet is used and then writes to XML stream. It seems overkilled to use...
5
8049
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, and need to manage all socket traffic. My thought was a stateful call to my own Encrypt and Decrypt routines. That I would pass a MemoryStream to the CryptoStream constructor. But it appears that for each write CryptoStream does to my memory...
2
2937
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 disk to a text file for future reference. I now have over 60 huge text files and would like to place the information into either a database or an excel document. I would like to read in from a text file and output the text file to an excel .cvs...
4
3757
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 having to use concatenation, which is awkward to write and to read: s.Format("{0," + fieldWidth.ToString() + "}", val); Preferably, I'd like to nest two placeholders, like this: s.Format("{0,{1}}", val, fieldWidth);
3
5431
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 have changed the state. I don't see the answer in Stroustrup or in the parashift FAQs. I would like to write a statement like my_ostream.restore_format_state_defaults();
4
9955
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. The trouble is that I'm not really sure how to do this in C# 2005. This was a very useful technique and if anyone could please show me how to adapt the Classic ASP code to C# 2005, I'd be very grateful indeed.
5
2163
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 date from Table 1 into
4
2356
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 // format cout<< "scientific:\t"<< 1234.56789<< '\n';
0
8454
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8362
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8560
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8644
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7389
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6200
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5671
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2012
muto222
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.