473,473 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 9212
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel | last post by:
in C# fastest way to convert a string into a MemoryStream
5
by: jk | last post by:
I'm having trouble converting a datatable into xml, with resonse.write to aspx. I'm basically converting vb code that saved a recordset into a stream into c#, but the format is wrong. I've tried...
0
by: Thaddeus | last post by:
//Author: //Thaddeus Jacobs, MCP //Kinematic Automation, Inc. //mailto:tjacobs@kinematic.com // //Description: //convert ADO .NET dataset to ADO 2.5 2.6 2.7 recordset and v/v //DataSet to...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
6
by: hb | last post by:
Hi, Would you please give me some idea to convert/decode a Base 64 encoded GIF image string to a *.gif file in ASP.Net? Thank you hb
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
11
by: Sudzzz | last post by:
Hi, I'm trying to convert a string something like this "{201,23,240,56,23,45,34,23}" into an array in C++ Please help. Thanks, Sudzzz
2
by: yogi_bear_79 | last post by:
I have a double of unknown length that I need to split at the decimal. I thought I would convert it either to a string or a char. char seems to be the best since it easily lends itself to...
4
by: Brad | last post by:
I am trying to convert an XML Stream received from a web api call into a DataSet to use in the rest of the app. The issue I am running into is that it will not convert the stream to a dataset and...
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
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...
0
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,...
0
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...
0
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...
0
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 ...
0
muto222
php
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.