473,395 Members | 1,766 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,395 software developers and data experts.

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.StringWriter (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.Stream. 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 26737
Tom,
Which leads me to my next question: What is the purpose of
System.IO.StringWriter (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.Stream. 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.Encoding) 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____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:E7**********************************@microsof t.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.StringWriter (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.Stream. 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.GetString( 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____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:87**********************************@microsof t.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.GetString( 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::stringstream 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____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:87**********************************@microsof t.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.GetString( 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
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...
2
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
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...
14
by: msnews.microsoft.com | last post by:
How can I encrypt and decrypt string?
3
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...
5
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...
7
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...
12
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" <<...
4
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;...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.