我正在努力解决通常的转换问题,但不幸的是我无法找到适合我的具体问题的任何内容。
我的应用程序正在从 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 字符。
我也读过一些答案,例如如何将 UTF-8 字符串转换为 Unicode? https://stackoverflow.com/questions/11293994/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"
the 从Utf8解码方法工作完美,问题是当我使用我的回复消息时。
任何建议将不胜感激