积极答复者
.net无法识别txt文件编码

问题
答案
-
那你设法先用If做这样的判断:http://www.2cto.com/kf/201208/149623.html(判断是否UTF8)。
Else部分用我DefaultEncoding方法。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已标记为答案 我心飞扬 2013年3月14日 6:47
-
public bool IsUTF8(System.IO.FileStream sbInputStream) { int i; byte cOctets; // octets to go in this UTF-8 encoded character byte chr; bool bAllAscii = true; long iLen = sbInputStream.Length; cOctets = 0; for (i = 0; i < iLen; i++) { chr = (byte)sbInputStream.ReadByte(); if ((chr & 0x80) != 0) bAllAscii = false; if (cOctets == 0) { if (chr >= 0x80) { do { chr <<= 1; cOctets++; } while ((chr & 0x80) != 0); cOctets--; if (cOctets == 0) return false; } } else { if ((chr & 0xC0) != 0x80) { return false; } cOctets--; } } if (cOctets > 0) { return false; } if (bAllAscii) { return false; } return true; }
最终是用以上的方法解决的,非常感谢大家的回答
- 已标记为答案 ThankfulHeartModerator 2013年3月14日 6:47
全部回复
-
你尝试另存文本文件(保存类型选择UTF-8):
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已建议为答案 ThankfulHeartModerator 2013年3月11日 3:13
- 取消建议作为答案 ThankfulHeartModerator 2013年3月13日 7:58
-
当你转换完成后请用notepad++ 或者 editplus打开看看 其编码方式是不是真的已经改成UTF-8了。
Mike Feng
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
那“我心飞扬”,麻烦你可以确认并标记答案吗?谢谢了。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
关键我是用程序去读取.txt 文件,不能另存为
那你必须保证一开始的内容是符合你条件编码的;)If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
你StringReader指定编码读取了吗?
string s = File.ReadAllText("d:\\1.txt", Encoding.GetEncoding("gb2312")); Console.WriteLine(s);
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
或许可以参考这个:
http://www.codeproject.com/Articles/17201/Detect-Encoding-for-In-and-Outgoing-Text
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
指定是可以的,关键我事先并不知道它是什么编码,这两个文件我判断不出来,根据前导符也不行,我好纠结啊
或者尝试:
string s = File.ReadAllText("d:\\1.txt", Encoding.Default); Console.WriteLine(s);
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
那你设法先用If做这样的判断:http://www.2cto.com/kf/201208/149623.html(判断是否UTF8)。
Else部分用我DefaultEncoding方法。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已标记为答案 我心飞扬 2013年3月14日 6:47
-
public bool IsUTF8(System.IO.FileStream sbInputStream) { int i; byte cOctets; // octets to go in this UTF-8 encoded character byte chr; bool bAllAscii = true; long iLen = sbInputStream.Length; cOctets = 0; for (i = 0; i < iLen; i++) { chr = (byte)sbInputStream.ReadByte(); if ((chr & 0x80) != 0) bAllAscii = false; if (cOctets == 0) { if (chr >= 0x80) { do { chr <<= 1; cOctets++; } while ((chr & 0x80) != 0); cOctets--; if (cOctets == 0) return false; } } else { if ((chr & 0xC0) != 0x80) { return false; } cOctets--; } } if (cOctets > 0) { return false; } if (bAllAscii) { return false; } return true; }
最终是用以上的方法解决的,非常感谢大家的回答
- 已标记为答案 ThankfulHeartModerator 2013年3月14日 6:47