Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert a string to stream

Randy
Guest
 
Posts: n/a
#1: Nov 15 '05
Hello,
Is there an easy way to convert a string to a stream?
Thanks



Randy
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Convert a string to stream


I found this...
Stream stream = new
MemoryStream(System.Text.ASCIIEncoding.GetBytes(pr intbuffer));
but when I compile, it gives me this...
An object reference is required for the nonstatic field, method, or property
'System.Text.Encoding.GetBytes(string)'

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

"Randy" <temp@temp.com> wrote in message
news:u4HSbsBjDHA.1544@tk2msftngp13.phx.gbl...[color=blue]
> Hello,
> Is there an easy way to convert a string to a stream?
> Thanks
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 15 '05

re: Convert a string to stream


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

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#4: Nov 15 '05

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.GetBytes(
string_var) );

Cheers,

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

"Randy" <temp@temp.com> wrote in message
news:u4HSbsBjDHA.1544@tk2msftngp13.phx.gbl...[color=blue]
> Hello,
> Is there an easy way to convert a string to a stream?
> Thanks
>
>[/color]


Randy
Guest
 
Posts: n/a
#5: Nov 15 '05

re: Convert a string to stream


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


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


Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#6: Nov 15 '05

re: Convert a string to stream




Hi Randy,

You are missing the ASCII property form ASCIIEncoding:
System.Text.ASCIIEncoding.ASCII.GetBytes("test");
instead of:
System.Text.ASCIIEncoding.GetBytes(printbuffer)


BTW, I posted before an incorrect post, BitConverter.GetBytes() 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.com> wrote in message
news:eAiM0vBjDHA.3192@TK2MSFTNGP11.phx.gbl...[color=blue]
> I found this...
> Stream stream = new
> MemoryStream(System.Text.ASCIIEncoding.GetBytes(pr intbuffer));
> 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.Encoding.GetBytes(string)'
>
> What does this mean?
> Thanks for any help...
>
> "Randy" <temp@temp.com> wrote in message
> news:u4HSbsBjDHA.1544@tk2msftngp13.phx.gbl...[color=green]
> > Hello,
> > Is there an easy way to convert a string to a stream?
> > Thanks
> >
> >[/color]
>
>[/color]


Randy
Guest
 
Posts: n/a
#7: Nov 15 '05

re: Convert a string to stream


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


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:u48Ay6BjDHA.1072@TK2MSFTNGP09.phx.gbl...[color=blue]
>
>
> Hi Randy,
>
> You are missing the ASCII property form ASCIIEncoding:
> System.Text.ASCIIEncoding.ASCII.GetBytes("test");
> instead of:
> System.Text.ASCIIEncoding.GetBytes(printbuffer)
>
>
> BTW, I posted before an incorrect post, BitConverter.GetBytes() 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.com> wrote in message
> news:eAiM0vBjDHA.3192@TK2MSFTNGP11.phx.gbl...[color=green]
> > I found this...
> > Stream stream = new
> > MemoryStream(System.Text.ASCIIEncoding.GetBytes(pr intbuffer));
> > 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.Encoding.GetBytes(string)'
> >
> > What does this mean?
> > Thanks for any help...
> >
> > "Randy" <temp@temp.com> wrote in message
> > news:u4HSbsBjDHA.1544@tk2msftngp13.phx.gbl...[color=darkred]
> > > Hello,
> > > Is there an easy way to convert a string to a stream?
> > > Thanks
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#8: Nov 15 '05

re: Convert a string to stream


<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin 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.ASCII;

- 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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread