String to Stream

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

    String to Stream

    I'm looking for a way to convert String to Stream. I don't want to have to
    write the string out to a file to get the stream. Does anyone know how?

    TIA


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: String to Stream

    Hayato,
    You cannot convert the String to a Stream per se.

    However! You can use the StringReader & StringWriter to read from & write to
    a String. If your IO routines expect TextReader & TextWriter you can then
    use Strings & Streams interchangeable . As TextReader is the base class for
    both StreamReader & StringReader. Same with TextWriter and StreamWriter &
    StringWriter.

    With the aid of the System.Text.Enc oding class you could convert the String
    into an Array of Bytes, which you can pass to the constructor of
    MemoryStream.

    Of course you are free to inherit from Stream to create a StringStream
    class.

    The StringReader & StringWriter is my first choice, then I use the others as
    appropriate.

    Hope this helps
    Jay

    "Hayato Iriumi" <hiriumi@hotmai l.com> wrote in message
    news:efSHFHtjDH A.744@tk2msftng p13.phx.gbl...[color=blue]
    > I'm looking for a way to convert String to Stream. I don't want to have to
    > write the string out to a file to get the stream. Does anyone know how?
    >
    > TIA
    >
    >[/color]


    Comment

    • Fergus Cooney

      #3
      Re: String to Stream

      Konichiwa Hayato,

      Have you seen the MemoryStream? It works with byte arrays so you will need
      to convert your string - which you can do with an Encoding.

      [Untested]
      Dim aBytes (Encoding.XXX.G etMaxByteCount) As Byte _
      = Encoding.XXX.Ge tBytes (sString)
      Dim strmMem As New MemoryStream (aBytes)

      Where XXX is Unicode, or Ascii or whatever your string is.

      If I may ask, what do you want to do with this Streamed string?

      Regards,
      Fergus.



      Comment

      • Hayato Iriumi

        #4
        Re: String to Stream

        Thank you very much! Memory stream works.

        "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
        news:#JCu8QtjDH A.1808@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Hayato,
        > You cannot convert the String to a Stream per se.
        >
        > However! You can use the StringReader & StringWriter to read from & write[/color]
        to[color=blue]
        > a String. If your IO routines expect TextReader & TextWriter you can then
        > use Strings & Streams interchangeable . As TextReader is the base class for
        > both StreamReader & StringReader. Same with TextWriter and StreamWriter &
        > StringWriter.
        >
        > With the aid of the System.Text.Enc oding class you could convert the[/color]
        String[color=blue]
        > into an Array of Bytes, which you can pass to the constructor of
        > MemoryStream.
        >
        > Of course you are free to inherit from Stream to create a StringStream
        > class.
        >
        > The StringReader & StringWriter is my first choice, then I use the others[/color]
        as[color=blue]
        > appropriate.
        >
        > Hope this helps
        > Jay
        >
        > "Hayato Iriumi" <hiriumi@hotmai l.com> wrote in message
        > news:efSHFHtjDH A.744@tk2msftng p13.phx.gbl...[color=green]
        > > I'm looking for a way to convert String to Stream. I don't want to have[/color][/color]
        to[color=blue][color=green]
        > > write the string out to a file to get the stream. Does anyone know how?
        > >
        > > TIA
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Hayato Iriumi

          #5
          Re: String to Stream

          Konnichiwa to you too. ;)

          I have a Windows Service that listens a certain port. When ASP .NET
          application sends a message to the Windows Service, it starts to do batch
          processing... Well, the reason I wanted to use MemoryStream was that I
          wanted to encrypt the message and then send the message out to Windows
          Service. I've been trying to do this, but I guess I took a wrong route... My
          plan was...

          (1) Encrypt the string and keep the encrypted string in memory.
          (2) Create byte array out of the encrypted string.
          (3) Write it into NetworkStream
          (4) Listener receives it as byte array
          (5) Construct encrypted string
          (6) Decrypt it.

          I haven't successfully been able to do this... Any suggestion?

          "Fergus Cooney" <filter-1@tesco.net> wrote in message
          news:#XpP2VtjDH A.1084@tk2msftn gp13.phx.gbl...[color=blue]
          > Konichiwa Hayato,
          >
          > Have you seen the MemoryStream? It works with byte arrays so you will[/color]
          need[color=blue]
          > to convert your string - which you can do with an Encoding.
          >
          > [Untested]
          > Dim aBytes (Encoding.XXX.G etMaxByteCount) As Byte _
          > = Encoding.XXX.Ge tBytes (sString)
          > Dim strmMem As New MemoryStream (aBytes)
          >
          > Where XXX is Unicode, or Ascii or whatever your string is.
          >
          > If I may ask, what do you want to do with this Streamed string?
          >
          > Regards,
          > Fergus.
          >
          >
          >[/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: String to Stream

            * "Hayato Iriumi" <hiriumi@hotmai l.com> scripsit:[color=blue]
            > I'm looking for a way to convert String to Stream. I don't want to have to
            > write the string out to a file to get the stream. Does anyone know how?[/color]

            Have a look at the 'System.IO.Memo ryStream' class. You can write the
            string using a 'StreamWriter'.

            --
            Herfried K. Wagner
            MVP ยท VB Classic, VB.NET
            <http://www.mvps.org/dotnet>

            Comment

            • Fergus Cooney

              #7
              Re: String to Stream

              Hi Hayato,

              It sounds like a reasonable plan. What's not working in it?

              Regards,
              Fergus


              Comment

              Working...