472,127 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

how to convert stream data to string

I am getting data from an FTP into a Stream object. Now I need to put this
data into a string so I can manipulate it. Can anyone show me some example
code?
Nov 27 '06 #1
3 8949
That depends on what your 'data' is. You'll probably want to move it from
the stream into a byte[] and pass it to System.Text.Encoding.* (like ASCII,
etc - depends on what your data is).

--
Adam Clauss
"tparks69" <tp******@discussions.microsoft.comwrote in message
news:CA**********************************@microsof t.com...
>I am getting data from an FTP into a Stream object. Now I need to put this
data into a string so I can manipulate it. Can anyone show me some
example
code?

Nov 28 '06 #2
The data is text from a text file from a FTP location... you wouldn't happen
to have a code example would you?

"Adam Clauss" wrote:
That depends on what your 'data' is. You'll probably want to move it from
the stream into a byte[] and pass it to System.Text.Encoding.* (like ASCII,
etc - depends on what your data is).

--
Adam Clauss
"tparks69" <tp******@discussions.microsoft.comwrote in message
news:CA**********************************@microsof t.com...
I am getting data from an FTP into a Stream object. Now I need to put this
data into a string so I can manipulate it. Can anyone show me some
example
code?


Nov 28 '06 #3
Hi,

You can feed the Stream to a StreamReader and use ReadToEnd to the data as
String (note the default encoding of UTF8, any other encoding needs to be
specified in the StreamReader constructor).

string data = "";
using (StreamReader sr = new StreamReader(Stream,
Encoding.GetEncoding("ISO-8859-1")))
{
data = sr.ReadToEnd();
}

As the file is transferred over a network you may instead want to use a
MemoryStream as temporary storage while reading the network stream, and at
the end use MemoryStream.ToArray() to get the byte[]. The byte array can
be transferred to string using the Encoding class.
Encoding.UTF8.GetString(byte[])

string data = "";
int bytesRead = 0;
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[8096];
while ((bytesRead = s.Read(buffer, 0, buffer.Length)) 0)
{
ms.Write(buffer, 0, bytesRead);
// optional feedback on progress
}
data = Encoding.UTF8.GetString(ms.ToArray());
}

The former sample is easy, but will hang during the transfer until the
entire stream is read. The latter sample is more complex, but has the
advantage of being able to give feedback on the progress of the file
transfer.
On Tue, 28 Nov 2006 05:03:01 +0100, tparks69
<tp******@discussions.microsoft.comwrote:
The data is text from a text file from a FTP location... you wouldn't
happen
to have a code example would you?

"Adam Clauss" wrote:
>That depends on what your 'data' is. You'll probably want to move it
from
the stream into a byte[] and pass it to System.Text.Encoding.* (like
ASCII,
etc - depends on what your data is).

--
Adam Clauss
"tparks69" <tp******@discussions.microsoft.comwrote in message
news:CA**********************************@microso ft.com...
>I am getting data from an FTP into a Stream object. Now I need to put
this
data into a string so I can manipulate it. Can anyone show me some
example
code?




--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by jk | last post: by
reply views Thread by Thaddeus | last post: by
10 posts views Thread by Nikolay Petrov | last post: by
11 posts views Thread by Sudzzz | last post: by
2 posts views Thread by yogi_bear_79 | last post: by
4 posts views Thread by Brad | last post: by

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.