在循环结束时,我得到一个List,我在里面看到每个索引:
这是因为文本是希伯来文。 编码代码为:65001
System.Text.Encoding.GetEncoding(65001)
public List<string> GetResponsers(string contents)
{
List<string> threadList = new List<string>();
int f = 0;
int startPos = 0;
while (true)
{
string firstTag = "<FONT CLASS='text16b'>";
//string firstTag = ";>";
string lastTag = "&n";
//string lastTag = ":בתאריך";
f = contents.IndexOf(firstTag, startPos);
if (f == -1)
{
break;
}
int g = contents.IndexOf(lastTag, f);
startPos = g + lastTag.Length;
string responser = contents.Substring(f + 2, g - f - 2);
threadList.Add(responser);
}
return threadList;
}
如何为每个响应者应用编码?
这是我尝试使用编码后的方法:
public List<string> GetResponsers(string contents)
{
List<string> threadList = new List<string>();
int f = 0;
int startPos = 0;
while (true)
{
string firstTag = "<FONT CLASS='text16b'>";
//string firstTag = ";>";
string lastTag = "&n";
//string lastTag = ":בתאריך";
f = contents.IndexOf(firstTag, startPos);
if (f == -1)
{
break;
}
int g = contents.IndexOf(lastTag, f);
startPos = g + lastTag.Length;
string responser = contents.Substring(f + 2, g - f - 2);
Encoding iso = Encoding.GetEncoding(65001);
byte[] isoBytes = iso.GetBytes(responser);
string ff = iso.GetString(isoBytes);
responser = ff;
threadList.Add(responser);
}
return threadList;
}
但它没有改变任何东西。
答案 0 :(得分:1)
&#34;它没有改变任何东西&#34;因为
Encoding iso = Encoding.GetEncoding(65001);
byte[] isoBytes = iso.GetBytes(responser);
string ff = iso.GetString(isoBytes)
最终什么都不做 - 就像&#34;添加10,删除10&#34; - 最后没有任何变化。从同一编码中使用时,GetBytes和GetString是反函数。
您应该在获取字符串数据的位置应用编码解码(因为由于编码不正确而没有丢失或更改)。
但是如果字符串已经使用不正确的编码进行了解码,您可以尝试使用Default编码来纠正问题以获取原始字节并使用本地编码对其进行正确编码:
byte[] originalBytes = Encoding.Default.GetBytes(mangledStr);
string correctStr = Encoding.GetEncoding(65001).GetString(originalBytes );
但是,如果根据原始编码只使用字符串的一部分,它可能会起作用,也可能不起作用。
最佳解决方案是在原始网络服务中使用本机支持您的本地网站的一些编码 - Unicode或UTF8。它将使您免于编码解码的任何问题。
PS :还要考虑到,当编码与系统编码不同时,VS调试器有时会遇到字符串正确表示的问题 - 以防万一你在调试器中检查字符串。这同样适用于某些控件和应用程序。