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

String to Stream

I'm looking for a way to convert String to Stream. I don't want to have to
write the string out to a file to get the stream. Does anyone know how?

TIA
Nov 20 '05 #1
6 19903
Hayato,
You cannot convert the String to a Stream per se.

However! You can use the StringReader & StringWriter to read from & write to
a String. If your IO routines expect TextReader & TextWriter you can then
use Strings & Streams interchangeable. As TextReader is the base class for
both StreamReader & StringReader. Same with TextWriter and StreamWriter &
StringWriter.

With the aid of the System.Text.Encoding class you could convert the String
into an Array of Bytes, which you can pass to the constructor of
MemoryStream.

Of course you are free to inherit from Stream to create a StringStream
class.

The StringReader & StringWriter is my first choice, then I use the others as
appropriate.

Hope this helps
Jay

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:ef*************@tk2msftngp13.phx.gbl...
I'm looking for a way to convert String to Stream. I don't want to have to
write the string out to a file to get the stream. Does anyone know how?

TIA

Nov 20 '05 #2
Konichiwa Hayato,

Have you seen the MemoryStream? It works with byte arrays so you will need
to convert your string - which you can do with an Encoding.

[Untested]
Dim aBytes (Encoding.XXX.GetMaxByteCount) As Byte _
= Encoding.XXX.GetBytes (sString)
Dim strmMem As New MemoryStream (aBytes)

Where XXX is Unicode, or Ascii or whatever your string is.

If I may ask, what do you want to do with this Streamed string?

Regards,
Fergus.

Nov 20 '05 #3
Thank you very much! Memory stream works.

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:#J**************@TK2MSFTNGP09.phx.gbl...
Hayato,
You cannot convert the String to a Stream per se.

However! You can use the StringReader & StringWriter to read from & write to a String. If your IO routines expect TextReader & TextWriter you can then
use Strings & Streams interchangeable. As TextReader is the base class for
both StreamReader & StringReader. Same with TextWriter and StreamWriter &
StringWriter.

With the aid of the System.Text.Encoding class you could convert the String into an Array of Bytes, which you can pass to the constructor of
MemoryStream.

Of course you are free to inherit from Stream to create a StringStream
class.

The StringReader & StringWriter is my first choice, then I use the others as appropriate.

Hope this helps
Jay

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:ef*************@tk2msftngp13.phx.gbl...
I'm looking for a way to convert String to Stream. I don't want to have to write the string out to a file to get the stream. Does anyone know how?

TIA


Nov 20 '05 #4
Konnichiwa to you too. ;)

I have a Windows Service that listens a certain port. When ASP .NET
application sends a message to the Windows Service, it starts to do batch
processing... Well, the reason I wanted to use MemoryStream was that I
wanted to encrypt the message and then send the message out to Windows
Service. I've been trying to do this, but I guess I took a wrong route... My
plan was...

(1) Encrypt the string and keep the encrypted string in memory.
(2) Create byte array out of the encrypted string.
(3) Write it into NetworkStream
(4) Listener receives it as byte array
(5) Construct encrypted string
(6) Decrypt it.

I haven't successfully been able to do this... Any suggestion?

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:#X**************@tk2msftngp13.phx.gbl...
Konichiwa Hayato,

Have you seen the MemoryStream? It works with byte arrays so you will need to convert your string - which you can do with an Encoding.

[Untested]
Dim aBytes (Encoding.XXX.GetMaxByteCount) As Byte _
= Encoding.XXX.GetBytes (sString)
Dim strmMem As New MemoryStream (aBytes)

Where XXX is Unicode, or Ascii or whatever your string is.

If I may ask, what do you want to do with this Streamed string?

Regards,
Fergus.

Nov 20 '05 #5
* "Hayato Iriumi" <hi*****@hotmail.com> scripsit:
I'm looking for a way to convert String to Stream. I don't want to have to
write the string out to a file to get the stream. Does anyone know how?


Have a look at the 'System.IO.MemoryStream' class. You can write the
string using a 'StreamWriter'.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
Hi Hayato,

It sounds like a reasonable plan. What's not working in it?

Regards,
Fergus
Nov 20 '05 #7

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?
4
by: TT (Tom Tempelaere) | last post by:
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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.