【发布时间】:2016-02-07 09:17:01
【问题描述】:
我正在为通常的转换问题而苦苦挣扎,但不幸的是,我找不到任何适合我的具体问题的东西。
我的应用正在接收来自 php 服务器的 System.Net.Http.HttpResponseMessage,UTF8 编码,包含一些字符,如 \u00c3\u00a0 (à),但我无法转换它们。
string message = await result.Content.ReadAsStringAsync();
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
string newmessage = Encoding.UTF8.GetString(messageBytes, 0, messageBytes.Length);
这只是我的尝试之一,但没有任何反应,生成的字符串仍然有 \u00c3\u00a0 字符。
我还阅读了一些答案,例如How to convert a UTF-8 string into Unicode?,但这个解决方案对我不起作用。这是解决方案代码:
public static string DecodeFromUtf8(this string utf8String)
{
// copy the string as UTF-8 bytes.
byte[] utf8Bytes = new byte[utf8String.Length];
for (int i=0;i<utf8String.Length;++i) {
//Debug.Assert( 0 <= utf8String[i] && utf8String[i] <= 255, "the char must be in byte's range");
utf8Bytes[i] = (byte)utf8String[i];
}
return Encoding.UTF8.GetString(utf8Bytes,0,utf8Bytes.Length);
}
DecodeFromUtf8("d\u00C3\u00A9j\u00C3\u00A0"); // déjà
我注意到,当我使用像
这样的简单字符串尝试上述解决方案时string str = "Comunit\u00c3\u00a0"
DecodeFromUtf8 方法完美运行,问题是当我使用我的响应消息时。
任何建议将不胜感激
【问题讨论】:
-
为什么不使用“newmessage”来连接变量“message”中的原始消息?
-
如果你指的是我删除的调试行,那是一个错字
标签: c# utf-8 character-encoding windows-phone-8.1 windows-applications