473,714 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ 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 9277
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.Enc oding.* (like ASCII,
etc - depends on what your data is).

--
Adam Clauss
"tparks69" <tp******@discu ssions.microsof t.comwrote in message
news:CA******** *************** ***********@mic rosoft.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.Enc oding.* (like ASCII,
etc - depends on what your data is).

--
Adam Clauss
"tparks69" <tp******@discu ssions.microsof t.comwrote in message
news:CA******** *************** ***********@mic rosoft.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(St ream,
Encoding.GetEnc oding("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.To Array() to get the byte[]. The byte array can
be transferred to string using the Encoding class.
Encoding.UTF8.G etString(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.G etString(ms.ToA rray());
}

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******@discu ssions.microsof t.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.Enc oding.* (like
ASCII,
etc - depends on what your data is).

--
Adam Clauss
"tparks69" <tp******@discu ssions.microsof t.comwrote in message
news:CA******* *************** ************@mi crosoft.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
716
by: Daniel | last post by:
in C# fastest way to convert a string into a MemoryStream
5
52684
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 using streams, datadoc, xmlreader, etc with no success. I need to convert it directly to a string or via a stream, but not in a file. Here's some sample code that gives me the bad format. I'll post the good format below that give me rowsets rather...
0
10263
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 recordset conversion. //Get RecordSet from Dataset //ds2rs DataSet2RecordSet
4
3298
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 errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
6
3998
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
8083
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
11
5050
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
2636
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 splitting. I have a few issues with this theory. Hard codeing char M works fine for my test double -0.5 But I would like it to be more versitile. The format "%2.2f" works fine with my test data, but that should also be versitile. inline void...
4
9424
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 halts the program. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= WebClient client = new WebClient(); string url="<url of the api>"; Stream data = client.OpenRead(url);
0
9319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9081
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9034
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6639
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4468
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...
0
4731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3164
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
2529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2113
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.