UTF8 <-> windows 1251 conversion

  • Thread starter Thread starter David
  • Start date Start date
D

David

hello... i've a little problem here... n00b question -))
so if you can help me...
the "output" string bellow, comes in UNICODE, but i want to get it on
windows-1251 (cytillic)
how can i do this?..

webResponse = (HttpWebResponse)webRequest.GetResponse();

Stream streamResponse = webResponse.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);

output = streamRead.ReadToEnd();
 
hello... i've a little problem here... n00b question -))
so if you can help me...
the "output" string bellow, comes in UNICODE, but i want to get it on
windows-1251 (cytillic)
how can i do this?..

webResponse = (HttpWebResponse)webRequest.GetResponse();

Stream streamResponse = webResponse.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);

output = streamRead.ReadToEnd();

Hi David,

After obtaining the output string you can convert it to the encoding of your
choice by using Encoding.Convert available under System.Text namespace.

Please check the following code snippet:
<CODE>

string str = "UTF8 Encoded string.";
Encoding srcEncodingFormat = Encoding.UTF8;
Encoding dstEncodingFormat = Encoding.GetEncoding("windows-1251");
byte [] originalByteString = srcEncodingFormat.GetBytes(str);
byte [] convertedByteString = Encoding.Convert(srcEncodingFormat,
dstEncodingFormat, originalByteString);
string finalString = dstEncodingFormat.GetString(convertedByteString);

</CODE>

Hope it will help.
 
David,

Here's an example taken from the VS docs:

// Create a 'WebRequest' object with the specified url.
WebRequest myWebRequest =
WebRequest.Create("http://www.constoso.com");

// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse();

// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myWebResponse.GetResponseStream();

Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

// Pipe the stream to a higher level stream reader with the
required encoding format.
StreamReader readStream = new StreamReader( ReceiveStream, encode
);
Console.WriteLine("\nResponse stream received");
Char[] read = new Char[256];

// Read 256 charcters at a time.
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");

while (count > 0)
{
// Dump the 256 characters on a string and display the
string onto the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}

Console.WriteLine("");
// Release the resources of stream object.
readStream.Close();

// Release the resources of response object.
myWebResponse.Close();

Best Regards
Johann Blake
 
10nx it, helped...
only added here:

StreamReader streamRead = new StreamReader(streamResponse,
Encoding.GetEncoding("windows-1251"));
 
Back
Top