How to convert a string to MemoryStream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ad

    How to convert a string to MemoryStream

    I have a string variable.
    How can I convert the string to MemoryStream?


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: How to convert a string to MemoryStream

    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]
    - mvp@spam.guard. caspershouse.co m

    "ad" <flying@wfes.tc c.edu.twwrote in message
    news:%23t4AWEjp GHA.1592@TK2MSF TNGP04.phx.gbl. ..
    >I have a string variable.
    How can I convert the string to MemoryStream?
    >

    Comment

    • Rafal M

      #3
      Re: How to convert a string to MemoryStream

      ad wrote:
      I have a string variable.
      How can I convert the string to MemoryStream?
      >
      >
      ..NET 2.0

      byte[] a = System.Text.Enc oding.GetEncodi ng("iso-8859-1").GetBytes("m y
      string");
      System.IO.Memor yStream m = new System.IO.Memor yStream(a);

      Comment

      • ad

        #4
        Re: How to convert a string to MemoryStream

        Thanks,
        But what does GetEncoding("is o-8859-1") mean?
        Can I use
        System.Text.Enc oding.Unicode.G etBytes(("my string");


        "Rafal M" <rafalm1980@gaz eta.pl???????:e 955e2$rkv$1@ine ws.gazeta.pl...
        ad wrote:
        >I have a string variable.
        >How can I convert the string to MemoryStream?
        >
        .NET 2.0
        >
        byte[] a = System.Text.Enc oding.GetEncodi ng("iso-8859-1").GetBytes("m y
        string");
        System.IO.Memor yStream m = new System.IO.Memor yStream(a);

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: How to convert a string to MemoryStream

          ad wrote:
          But what does GetEncoding("is o-8859-1") mean?
          It means to use the ISO-Latin-1 encoding.
          Can I use
          System.Text.Enc oding.Unicode.G etBytes(("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.Unicod e you
          will always see two bytes for each character. Using
          Encoding.GetEnc oding("iso-8859-1") you will always see one byte per
          character, but many characters will not be represented correctly.

          Jon

          Comment

          Working...