PC Review


Reply
Thread Tools Rate Thread

How to convert a string to MemoryStream

 
 
ad
Guest
Posts: n/a
 
      13th Jul 2006
I have a string variable.
How can I convert the string to MemoryStream?


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      13th Jul 2006
ad,

Attach the MemoryStream to a StreamWriter, and then call one of the
Write methods on the StreamWriter. This will convert using the Encoding set
on the StreamWriter, and convert your characters to the byte stream.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"ad" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I have a string variable.
> How can I convert the string to MemoryStream?
>



 
Reply With Quote
 
Rafal M
Guest
Posts: n/a
 
      13th Jul 2006
ad wrote:
> I have a string variable.
> How can I convert the string to MemoryStream?
>
>


..NET 2.0

byte[] a = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes("my
string");
System.IO.MemoryStream m = new System.IO.MemoryStream(a);
 
Reply With Quote
 
ad
Guest
Posts: n/a
 
      14th Jul 2006
Thanks,
But what does GetEncoding("iso-8859-1") mean?
Can I use
System.Text.Encoding.Unicode.GetBytes(("my string");


"Rafal M" <(E-Mail Removed)> ???????:e955e2$rkv$(E-Mail Removed)...
> ad wrote:
>> I have a string variable.
>> How can I convert the string to MemoryStream?

>
> .NET 2.0
>
> byte[] a = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes("my
> string");
> System.IO.MemoryStream m = new System.IO.MemoryStream(a);



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th Jul 2006
ad wrote:
> But what does GetEncoding("iso-8859-1") mean?


It means to use the ISO-Latin-1 encoding.

> Can I use
> System.Text.Encoding.Unicode.GetBytes(("my string");


Absolutely. It'll give you different results though. A stream
inherently deals with binary data, and a string is comprised of
character data. The encoding you use specifies the conversion from the
character data to binary data. For instance, using Encoding.Unicode you
will always see two bytes for each character. Using
Encoding.GetEncoding("iso-8859-1") you will always see one byte per
character, but many characters will not be represented correctly.

Jon

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MemoryStream into String info@listerhome.com Microsoft C# .NET 4 5th Apr 2008 07:45 PM
in C# fastest way to convert a string into a MemoryStream Daniel Microsoft Dot NET 5 3rd Aug 2005 05:32 PM
in C# fastest way to convert a string into a MemoryStream Octavio Hernandez Microsoft C# .NET 2 3rd Aug 2005 07:38 AM
in C# fastest way to convert a string into a MemoryStream Daniel Microsoft Dot NET Framework 1 3rd Aug 2005 07:17 AM
Convert memoryStream to unicode string =?Utf-8?B?RWRp?= Microsoft Dot NET Framework 1 25th Jul 2005 01:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:17 AM.