Convert a string to stream

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

    #1

    Convert a string to stream

    Hello,
    Is there an easy way to convert a string to a stream?
    Thanks


  • Randy

    #2
    Re: Convert a string to stream

    I found this...
    Stream stream = new
    MemoryStream(Sy stem.Text.ASCII Encoding.GetByt es(printbuffer) );
    but when I compile, it gives me this...
    An object reference is required for the nonstatic field, method, or property
    'System.Text.En coding.GetBytes (string)'

    What does this mean?
    Thanks for any help...

    "Randy" <temp@temp.co m> wrote in message
    news:u4HSbsBjDH A.1544@tk2msftn gp13.phx.gbl...[color=blue]
    > Hello,
    > Is there an easy way to convert a string to a stream?
    > Thanks
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Convert a string to stream

      Randy <temp@temp.co m> wrote:[color=blue]
      > Is there an easy way to convert a string to a stream?[/color]

      Does System.IO.Strin gReader does what you want? If not, you could use
      an Encoding to get the bytes from the string, then
      System.IO.Memor yStream to build a stream from those bytes.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Ignacio Machin \( .NET/ C#  MVP \)

        #4
        Re: Convert a string to stream

        Hi Randy?

        What you mean with that?

        The best I can think of would be like

        MemoryStream mstream = new MemoryStream( BitConverter.Ge tBytes(
        string_var) );

        Cheers,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation

        "Randy" <temp@temp.co m> wrote in message
        news:u4HSbsBjDH A.1544@tk2msftn gp13.phx.gbl...[color=blue]
        > Hello,
        > Is there an easy way to convert a string to a stream?
        > Thanks
        >
        >[/color]


        Comment

        • Randy

          #5
          Re: Convert a string to stream

          StringReader seems to work great! Thanks for your help...


          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.19eb96 064051340a9897f 1@msnews.micros oft.com...[color=blue]
          > Randy <temp@temp.co m> wrote:[color=green]
          > > Is there an easy way to convert a string to a stream?[/color]
          >
          > Does System.IO.Strin gReader does what you want? If not, you could use
          > an Encoding to get the bytes from the string, then
          > System.IO.Memor yStream to build a stream from those bytes.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Ignacio Machin \( .NET/ C#  MVP \)

            #6
            Re: Convert a string to stream



            Hi Randy,

            You are missing the ASCII property form ASCIIEncoding:
            System.Text.ASC IIEncoding.ASCI I.GetBytes("tes t");
            instead of:
            System.Text.ASC IIEncoding.GetB ytes(printbuffe r)


            BTW, I posted before an incorrect post, BitConverter.Ge tBytes() does not
            have an overload for string , please ignore it.

            Cheers,

            --
            Ignacio Machin,
            ignacio.machin AT dot.state.fl.us
            Florida Department Of Transportation

            "Randy" <temp@temp.co m> wrote in message
            news:eAiM0vBjDH A.3192@TK2MSFTN GP11.phx.gbl...[color=blue]
            > I found this...
            > Stream stream = new
            > MemoryStream(Sy stem.Text.ASCII Encoding.GetByt es(printbuffer) );
            > but when I compile, it gives me this...
            > An object reference is required for the nonstatic field, method, or[/color]
            property[color=blue]
            > 'System.Text.En coding.GetBytes (string)'
            >
            > What does this mean?
            > Thanks for any help...
            >
            > "Randy" <temp@temp.co m> wrote in message
            > news:u4HSbsBjDH A.1544@tk2msftn gp13.phx.gbl...[color=green]
            > > Hello,
            > > Is there an easy way to convert a string to a stream?
            > > Thanks
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Randy

              #7
              Re: Convert a string to stream

              Oh yeah...thanks for the info. I see now...


              "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
              in message news:u48Ay6BjDH A.1072@TK2MSFTN GP09.phx.gbl...[color=blue]
              >
              >
              > Hi Randy,
              >
              > You are missing the ASCII property form ASCIIEncoding:
              > System.Text.ASC IIEncoding.ASCI I.GetBytes("tes t");
              > instead of:
              > System.Text.ASC IIEncoding.GetB ytes(printbuffe r)
              >
              >
              > BTW, I posted before an incorrect post, BitConverter.Ge tBytes() does not
              > have an overload for string , please ignore it.
              >
              > Cheers,
              >
              > --
              > Ignacio Machin,
              > ignacio.machin AT dot.state.fl.us
              > Florida Department Of Transportation
              >
              > "Randy" <temp@temp.co m> wrote in message
              > news:eAiM0vBjDH A.3192@TK2MSFTN GP11.phx.gbl...[color=green]
              > > I found this...
              > > Stream stream = new
              > > MemoryStream(Sy stem.Text.ASCII Encoding.GetByt es(printbuffer) );
              > > but when I compile, it gives me this...
              > > An object reference is required for the nonstatic field, method, or[/color]
              > property[color=green]
              > > 'System.Text.En coding.GetBytes (string)'
              > >
              > > What does this mean?
              > > Thanks for any help...
              > >
              > > "Randy" <temp@temp.co m> wrote in message
              > > news:u4HSbsBjDH A.1544@tk2msftn gp13.phx.gbl...[color=darkred]
              > > > Hello,
              > > > Is there an easy way to convert a string to a stream?
              > > > Thanks
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Convert a string to stream

                <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
                dot.state.fl.us >> wrote:[color=blue]
                > You are missing the ASCII property form ASCIIEncoding:[/color]

                Note that it's actually a property of Encoding, not ASCIIEncoding -
                it's just visible through ASCIIEncoding as well, of course. I
                personally prefer to read:

                Encoding ascii = Encoding.ASCII;

                rather than

                Encoding ascii = ASCIIEncoding.A SCII;

                - the latter gives the impression that the property is a member of the
                ASCIIEncoding class.

                (Obviously the same goes for the other built-in encodings.)

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                Working...