473,473 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Encoding... sigh.

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
10 1279
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
"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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Christopher H. Laco | last post by:
Long story longer. I need to get web user input into a backend system that a) only grocks single byte encoding, b) expectes the data transer to be 1 bytes = 1 character, and c) uses the HP Roman-6...
6
by: | last post by:
Hi, I'm using .Net CF, and I'm trying to load an XML document that has a first line: <?xml version="1.0" encoding="ISO-8859-1"?> I'm loading this via XmlDocument.Load(). However, I get and...
8
by: Demon News | last post by:
I'm trying to do a transform (Using XmlTransform class in c#) and in the Transform I'm specifying the the output xsl below: <xsl:output method="xml" encoding="UTF-8" indent="no"/> the...
4
by: fitsch | last post by:
Hi, I am trying to write a generic RSS/Atom/OPML feed client. The problem is, that those xml feeds may have different encodings: - <?xml version="1.0" encoding="ISO-8859-1" ?>... - <?xml...
7
by: sammy | last post by:
ok... so i restarted my server and i get a "Server Application Unavailable The web application you are attempting to access on this web server is currently unavailable. Please hit the "Refresh"...
0
by: Chris McDonough | last post by:
ElementTree's XML serialization routine implied by tree._write(file, node, encoding, namespaces looks like this (elided): def _write(self, file, node, encoding, namespaces): # write XML to file...
4
by: Christina | last post by:
Hey Guys, Currently, I am using the below code: Dim oReqDoc as XmlDocument Dim requiredBytes As Byte() requiredBytes = System.Text.UTF8Encoding.UTF8.GetBytes(oReqDoc.InnerXml). Here, I am...
3
by: mortb | last post by:
1. How do I determine which encoding a xmldocument or xmlreader uses when opening a document? I'm not just talking about the <?xml encoding="utf-8"?attribute, but the actual encoding of the...
1
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.