Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8200

convert byte[] to string with charset Encoding in C#

I'm new to Windows phone 8 Dev and C#. I try to get String from remote server. This is my code:

  var client = new HttpClient();
  var response = await client.GetAsync(MyUrl);
  string charset = GetCharset(response.Content.Headers.ContentType.ToString());
  Debug.WriteLine("charset: " + charset); \\debug show: charset: ISO-8859-1
  var bytesCharset = await response.Content.ReadAsByteArrayAsync();
  Debug.WriteLine(Encoding.UTF8.GetString(bytesCharset,0,bytesCharset.Length)); 
  //Debug show: `m\u1EF9 t�m`

m\u1EF9 t�m is what I got, but I expect it's Vietnamese: Mỹ Tâm. Can anyone give me an idea to get expected result? (I think the bytes is encoded in IOS-8859-1).

Upvotes: 2

Views: 1527

Answers (3)

Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8200

Finally I found the solution. After using Json.Net to parse json from remote server, my problem's gone, and I can get expected result:

 JArray jArray = JArray.Parse(isoText);
 String exptectedText= jArray[1].ToString();

Actually, I dont know why my code works :( Thank you anyway!

Upvotes: 0

gruntledcustomer
gruntledcustomer

Reputation: 253

Encoding.UTF8 should be interpreting your data correctly, as ISO-8859-1 is a subset of UTF-8. However, so that other encodings are handled transparently, it is probably better to use HttpContent's ReadAsStringAsync() method instead of ReadAsByteArrayAsync().

I suspect you have the correct string there but your debug window simply cannot display those two unicode characters properly.

Upvotes: 2

Wolfgang Ziegler
Wolfgang Ziegler

Reputation: 1685

You can use the static GetEncoding method to request a specificy codepage. e.g.:

var encoding = Encoding.GetEncoding("ISO-8859-1");

Upvotes: 0

Related Questions