none
Convert String to Stream?

    Question

  • I have a string, actually a Base64 string that I need to get into a Stream and am struggling to get it right.

    I'm starting by using Convert.FromBase64String(myString) to get a byte[], but I can't seem to figure out how to get that into a Stream either.
    Thursday, April 16, 2009 6:08 PM

Answers

  • MemoryStream myStream = new MemoryStream (Convert.FromBase64String(myString));

    Regards.
    /* No comments */
    Thursday, April 16, 2009 6:11 PM
  • using System;
    using System.IO;
    using System.Text;

    namespace ConsoleApplication
    {
        class Program
        {
            static void Main( string[] args )
            {
                string test = "Testing 1-2-3";

                // convert string to stream
                byte[] byteArray = Convert.FromBase64String( test );
                MemoryStream stream = new MemoryStream( byteArray );

                // convert stream to string
                StreamReader reader = new StreamReader( stream );
                string text = reader.ReadToEnd();

                Console.WriteLine( text );
                Console.ReadLine();
            }
        }
    }

    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    • Marked as answer by Harry Zhu Wednesday, April 22, 2009 3:53 AM
    Thursday, April 16, 2009 6:20 PM
  • MemoryStream stream = new MemoryStream(Encoding.Default.GetBytes(str));


    David Morton - http://blog.davemorton.net/
    Thursday, April 16, 2009 6:22 PM
    Moderator

All replies

  • MemoryStream myStream = new MemoryStream (Convert.FromBase64String(myString));

    Regards.
    /* No comments */
    Thursday, April 16, 2009 6:11 PM
  • using System;
    using System.IO;
    using System.Text;

    namespace ConsoleApplication
    {
        class Program
        {
            static void Main( string[] args )
            {
                string test = "Testing 1-2-3";

                // convert string to stream
                byte[] byteArray = Convert.FromBase64String( test );
                MemoryStream stream = new MemoryStream( byteArray );

                // convert stream to string
                StreamReader reader = new StreamReader( stream );
                string text = reader.ReadToEnd();

                Console.WriteLine( text );
                Console.ReadLine();
            }
        }
    }

    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    • Marked as answer by Harry Zhu Wednesday, April 22, 2009 3:53 AM
    Thursday, April 16, 2009 6:20 PM
  • MemoryStream stream = new MemoryStream(Encoding.Default.GetBytes(str));


    David Morton - http://blog.davemorton.net/
    Thursday, April 16, 2009 6:22 PM
    Moderator