473,800 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String stream in .NET???

Hey there,

I need a string stream, but I can't find one in .NET. I thought StringWriter
would derive from Stream, alas it doesn't do so.

Which leads me to my next question: What is the purpose of
System.IO.Strin gWriter (or, what is its added value)??? It can do the same
things with StringBuilder, and I see no added value.

The reason why I need the string stream, is that I have a library that knows
how to serialize certain library objects to a System.IO.Strea m. My intent is
to serialize to a string, but I cannot do that with the current interface.

I do know that there is a memory stream. But my other string ouput methods
use StringBuilder.. ., not any preallocated byte buffer.

What I would prefer, is a StringStream class that derives from Stream, and
takes a StringBuilder as scratch-pad.

Any comment is welcome.

Thanks,
--
Tom Tempelaere.
Nov 16 '05 #1
4 26815
Tom,
Which leads me to my next question: What is the purpose of
System.IO.Strin gWriter (or, what is its added value)??? StringWriter inherits from TextWriter, it can be used any place a TextWriter
can be used. Creating functions that expect a TextWriter allows you to write
to either a Stream or a String...

The reason why I need the string stream, is that I have a library that
knows
how to serialize certain library objects to a System.IO.Strea m. My intent
is
to serialize to a string, but I cannot do that with the current interface. Remember that a String is a series of Unicode Chars (16 bit values). While a
Stream is a series of Bytes (8 bit values).
What I would prefer, is a StringStream class that derives from Stream, and
takes a StringBuilder as scratch-pad. You could define one, however I wonder how well the encoding
(System.Text.En coding) would work, as a series of Bytes is not necessarily
in any specific encoding, in fact a series of Bytes may be in multiple
encodings!

I do know that there is a memory stream. But my other string ouput methods
use StringBuilder.. ., not any preallocated byte buffer. A Memory stream does not need to be preallocated, it can dynamically grow
just as a StringBuilder.
I would recommend using the MemoryStream when I wanted to serialize to
memory...

Hope this helps
Jay

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThot mailD.Tcom|/\|@P$0_|\|_>
wrote in message news:E7******** *************** ***********@mic rosoft.com... Hey there,

I need a string stream, but I can't find one in .NET. I thought
StringWriter
would derive from Stream, alas it doesn't do so.

Which leads me to my next question: What is the purpose of
System.IO.Strin gWriter (or, what is its added value)??? It can do the same
things with StringBuilder, and I see no added value.

The reason why I need the string stream, is that I have a library that
knows
how to serialize certain library objects to a System.IO.Strea m. My intent
is
to serialize to a string, but I cannot do that with the current interface.

I do know that there is a memory stream. But my other string ouput methods
use StringBuilder.. ., not any preallocated byte buffer.

What I would prefer, is a StringStream class that derives from Stream, and
takes a StringBuilder as scratch-pad.

Any comment is welcome.

Thanks,
--
Tom Tempelaere.

Nov 16 '05 #2
Hi Jay,

Thanks for the response.

What should I do to get a string if I would serialize to a MemoryStream?
I've tried:

string s = encoding.GetStr ing( ms.GetBuffer(), 0, (int) ms.Length );

but all that comes out is crap. I've tried ASCIIEncoding and UTF8 and UTF7.
I guess there is no good way...

Thanks,
Tom.
Nov 16 '05 #3
Tom,
but all that comes out is crap. That's what I would expect you will see. As serialization to a stream is
more then likely Binary serialization. Binary serialization has no really
textual representation.

TextWriter (along with its descendents StringWriter & StreamWriter) are used
to create textual representation of objects. If you are writing to a Stream
this "Textual Representation" cannot magically happen by simply using a
StringWriter...
What should I do to get a string if I would serialize to a MemoryStream?
What are you expecting the string to look like? A String is text that people
can read, a Stream is bytes that people cannot read (well people can read
them, but they tend to have all sorts of funny characters in random order).

Hope this helps
Jay

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThot mailD.Tcom|/\|@P$0_|\|_>
wrote in message news:87******** *************** ***********@mic rosoft.com... Hi Jay,

Thanks for the response.

What should I do to get a string if I would serialize to a MemoryStream?
I've tried:

string s = encoding.GetStr ing( ms.GetBuffer(), 0, (int) ms.Length );

but all that comes out is crap. I've tried ASCIIEncoding and UTF8 and
UTF7.
I guess there is no good way...

Thanks,
Tom.

Nov 16 '05 #4
Jay,

Thank you for sharing your insight. I now have a clearer view. The
serialization methods in the library were indeed for binary serialization and
not for string serialization.

I had to write my own string serialization routine.

Perhaps I made a mistake in comparing the use of std::stringstre am in C++ to
streams in general.

Thank you,
Tom Tempelaere.

"Jay B. Harlow [MVP - Outlook]" wrote:
Tom,
but all that comes out is crap.

That's what I would expect you will see. As serialization to a stream is
more then likely Binary serialization. Binary serialization has no really
textual representation.

TextWriter (along with its descendents StringWriter & StreamWriter) are used
to create textual representation of objects. If you are writing to a Stream
this "Textual Representation" cannot magically happen by simply using a
StringWriter...
What should I do to get a string if I would serialize to a MemoryStream?


What are you expecting the string to look like? A String is text that people
can read, a Stream is bytes that people cannot read (well people can read
them, but they tend to have all sorts of funny characters in random order).

Hope this helps
Jay

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThot mailD.Tcom|/\|@P$0_|\|_>
wrote in message news:87******** *************** ***********@mic rosoft.com...
Hi Jay,

Thanks for the response.

What should I do to get a string if I would serialize to a MemoryStream?
I've tried:

string s = encoding.GetStr ing( ms.GetBuffer(), 0, (int) ms.Length );

but all that comes out is crap. I've tried ASCIIEncoding and UTF8 and
UTF7.
I guess there is no good way...

Thanks,
Tom.


Nov 16 '05 #5

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

Similar topics

16
4888
by: Der Andere | last post by:
During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams. However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that). Cheers, Matthias -- Für emails Anweisung in der Adresse befolgen
2
1369
by: JM | last post by:
Hi, If you change the same string stream to a new string, why is it that you have to .clear() it before using it? If I don't I get garbage. Thanks, Jason ex.
4
1497
by: dave | last post by:
hi im looking for a down n dirty blazing fast stuff function or sample how to write one my self for example: newstring=stuff(origstring,nstart,chrsdel,newinsert) 1. orgstring is the original for instance a page from a document 2 beginning position 3. how many chars to delete 4. string,chars to insert at Nstart point thanks for all the help
14
7701
by: msnews.microsoft.com | last post by:
How can I encrypt and decrypt string?
3
1403
by: Bob Altman | last post by:
Hi all, Why doesn't the following unmanaged C++ code work as expected: string s; ostringstream strm(s); // This stream should store results in s strm << 25; cout << s << endl; // s still contains an empty string cout << strm.str(); // but the stream internally contains "25"
5
9257
by: Paul Aspinall | last post by:
Hi I want to send an ASCII character string / stream to an IP address. I basically have 6 barcode printers, and a web interface. Depending on what is entered on the web page, will determine which printer the label is printed on (ie. which IP address the ASCII string / stream is sent to). How can I send an ASCII string / stream to an IP address??
7
9994
by: Lee | last post by:
Hi, I'm a stream virgin and am attempting to output strings to a file. My approach is to write the string initially to a 'stringstream' and only when complete write the stringstream to the file (ofstream). The process works fine however appears to be rather slow. For example outputting about 2Mb of data takes a couple of minutes (most of the time appears to be writing to the stringstream) and as I'm creating several hundred files in...
12
2696
by: gato | last post by:
Hello Everybody, I want my own class to behave like the following oss object; ------------------------ code begin ----- int main () { int i = 67; ostringstream oss; oss << "A" << i << endl;
4
2411
MrPickle
by: MrPickle | last post by:
I am tokenizing a line using a stringstream and a vector but the stringstream seems to be repeating the first word, here's my code: std::stringstream ss; std::vector<std::string> LineVec; std::string Line, Word; while(std::getline(File, Line)) { if(Line.empty()) { continue; } ss.str(Line); ss.seekg(std::ios::beg);
0
9690
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
9551
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,...
0
10274
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9085
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...
0
6811
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
5469
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
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 we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.