【问题标题】:How to decode this string to utf-8 string? [closed]如何将此字符串解码为 utf-8 字符串? [关闭]
【发布时间】:2013-04-05 14:30:05
【问题描述】:

我有这个代码:

string URL = "http://translate.google.com/m?hl=vi&sl=en&tl=vi&ie=UTF-8&prev=_m&q=song";
string s = "";
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/5.0 (SymbianOS/9.4; U; Series60/5.0 Nokia5800d-1/21.0.025; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413");
s = client.DownloadString(URL);

运行时我收到一个格式为该字符串的字符串:

danh từ buổi há»p để ca hát

到这个字符串:

danh từ buổi họp để ca hát

非常感谢:)

【问题讨论】:

  • 原件从何而来?它被编码成什么?
  • 对。页面的编码是什么,为什么不将client.Encoding 设置为开头?
  • @Oded:谢谢,成功了,我是新手 :)

标签: c# string utf-8 decode


【解决方案1】:

在获取代表字符串的byte[] 时,需要在获取之前知道使用了什么编码。

在这种情况下,它是 UTF-8,因此将 WebClient 的 Encoding 设置为正确的字符串:

string URL = "http://translate.google.com/m?hl=vi&sl=en&tl=vi&ie=UTF-8&prev=_m&q=song";
string s = "";
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
client.Headers.Add("user-agent", "Mozilla/5.0 (SymbianOS/9.4; U; Series60/5.0 Nokia5800d-1/21.0.025; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413");
s = client.DownloadString(URL);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-23
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多