By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,460 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,460 IT Pros & Developers. It's quick & easy.

Encoding... sigh.

P: n/a
response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),
Encoding.Default);
string s = sr.ReadToEnd();

When the above is used to download an image, say, the image's bytes
get messed up. How can I do the above *without* messing with the
bytes?
Nov 15 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
I don't think that you can assign the byte stream to a string at any rate..

What you really want to do?

Nirosh.

"C# Learner" <cs****@learner.here> wrote in message
news:2j********************************@4ax.com...
response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),
Encoding.Default);
string s = sr.ReadToEnd();

When the above is used to download an image, say, the image's bytes
get messed up. How can I do the above *without* messing with the
bytes?

Nov 15 '05 #2

P: n/a
"Champika Nirosh" <no****@textcentric.lk> wrote:
I don't think that you can assign the byte stream to a string at any rate..
The code I wrote works, but not if there are any bytes in the stream
that are out of the range of whatever encoding is used.
What you really want to do?
I'm trying to get the *raw* bytes of what's in the stream. In effect,
I'm trying to download data over HTTP, *and* be able able to
manipulate the raw bytes.
Nirosh.


Regards
Nov 15 '05 #3

P: n/a
As you said, Yes your code may work but it won't show you the actual content
as you assign the byte stream into string.

For manupulation you can use the stream reader itself, same time it would be
easier if you can load the byte stream to a System.IO.Stream class

Nirosh.

"C# Learner" <cs****@learner.here> wrote in message
news:7f********************************@4ax.com...
"Champika Nirosh" <no****@textcentric.lk> wrote:
I don't think that you can assign the byte stream to a string at any
rate..
The code I wrote works, but not if there are any bytes in the stream
that are out of the range of whatever encoding is used.
What you really want to do?


I'm trying to get the *raw* bytes of what's in the stream. In effect,
I'm trying to download data over HTTP, *and* be able able to
manipulate the raw bytes.
Nirosh.


Regards

Nov 15 '05 #4

P: n/a
C# Learner <cs****@learner.here> wrote:
response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),
Encoding.Default);
string s = sr.ReadToEnd();

When the above is used to download an image, say, the image's bytes
get messed up. How can I do the above *without* messing with the
bytes?


You don't. You don't mix binary data and character data. Strings are
for character data, *not* binary data.

See http://www.pobox.com/~skeet/csharp/encoding.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
C# Learner <cs****@learner.here> wrote:
What you really want to do?


I'm trying to get the *raw* bytes of what's in the stream.


Then just read the content from the stream, and don't try to convert it
into a string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
C# Learner <cs****@learner.here> wrote:
>What you really want to do?


I'm trying to get the *raw* bytes of what's in the stream.


Then just read the content from the stream, and don't try to convert it
into a string.


But I need to eventually convert this content into an array of bytes
(to be sent off to another socket). I really can't have decoding
happening here. :-(

Thanks.
Nov 15 '05 #7

P: n/a
C# Learner <cs****@learner.here> wrote:
Then just read the content from the stream, and don't try to convert it
into a string.


But I need to eventually convert this content into an array of bytes
(to be sent off to another socket). I really can't have decoding
happening here. :-(


Then why were you trying to read it into a string? Just read it into an
array of bytes. You can use MemoryStream to make that easier for you -
just read a block at a time, writing the contents of the block to the
MemoryStream, and then call ToArray at the end.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
C# Learner <cs****@learner.here> wrote:
>Then just read the content from the stream, and don't try to convert it
>into a string.


But I need to eventually convert this content into an array of bytes
(to be sent off to another socket). I really can't have decoding
happening here. :-(


Then why were you trying to read it into a string? Just read it into an
array of bytes. You can use MemoryStream to make that easier for you -
just read a block at a time, writing the contents of the block to the
MemoryStream, and then call ToArray at the end.


Can you show me an example please? I don't quite understand how I'd
do that.

Thanks
Nov 15 '05 #9

P: n/a
C# Learner <cs****@learner.here> wrote:
Then why were you trying to read it into a string? Just read it into an
array of bytes. You can use MemoryStream to make that easier for you -
just read a block at a time, writing the contents of the block to the
MemoryStream, and then call ToArray at the end.


Can you show me an example please? I don't quite understand how I'd
do that.


See http://www.pobox.com/~skeet/csharp/readbinary.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
C# Learner <cs****@learner.here> wrote:
>Then why were you trying to read it into a string? Just read it into an
>array of bytes. You can use MemoryStream to make that easier for you -
>just read a block at a time, writing the contents of the block to the
>MemoryStream, and then call ToArray at the end.


Can you show me an example please? I don't quite understand how I'd
do that.


See http://www.pobox.com/~skeet/csharp/readbinary.html


Thanks Jon, works fine.
Nov 15 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.