Objective
C # in the identification of file encoding is a headache, the recent introduction of micro-letter merchants in the background refund data, no matter how to set the code to export are garbled, and then found on the Internet to identify the code of the file coding, feel good. Finally recognized is gb2312, it seems that I still too slag, can only eat soil, unexpectedly forgot this code.
The following words do not say much, on the code.
<summary>///is used to get the encoding of a text file (Encoding).
</summary> public class Txtfileencoder {public Txtfileencoder () {////TODO: Add constructor logic here// ///<summary>///Gets the encoding of a text file.
If a valid leader is not found on the file's head, Encoding.default is returned. </summary>///<param name= "filename" > filename. </param>///<returns></returns> public static Encoding getencoding (string fileName) {return
GetEncoding (FileName, Encoding.default);
///<summary>///Gets the encoding of a text file stream. </summary>///<param name= "Stream" > Text file stream. </param>///<returns></returns> public static Encoding getencoding (FileStream stream) {Retu
RN GetEncoding (stream, Encoding.default);
///<summary>///Gets the encoding of a text file. </summary>///<param name= "filename" > filename. </param>///<param name= "defaultencoding" > Default encoding method. This encoding is returned when the method cannot obtain a valid leader from the head of the file. </param>/// <returns></returns> public static Encoding getencoding (String fileName, Encoding defaultencoding) {F
Ilestream fs = new FileStream (FileName, FileMode.Open);
Encoding targetencoding = getencoding (FS, defaultencoding); Fs.
Close ();
return targetencoding;
///<summary>///Gets the encoding of a text file stream. </summary>///<param name= "Stream" > Text file stream. </param>///<param name= "defaultencoding" > Default encoding method. This encoding is returned when the method cannot obtain a valid leader from the head of the file. </param>///<returns></returns> public static Encoding getencoding (FileStream stream, Encoding de
faultencoding) {Encoding targetencoding = defaultencoding; if (stream!= null && stream.
Length >= 2) {//Save the first 4 bytes of the file stream byte byte1 = 0;
byte Byte2 = 0;
byte byte3 = 0;
byte byte4 = 0; Saves the current seek location long Origpos = stream.
Seek (0, seekorigin.begin); Stream.
Seek (0, seekorigin.begin); int nbyte = stream.
ReadByte (); Byte1 = ConveRt.
ToByte (Nbyte); Byte2 = Convert.tobyte (stream.
ReadByte ()); if (stream. Length >= 3) {byte3 = Convert.tobyte (stream.
ReadByte ()); } if (stream. Length >= 4) {byte4 = Convert.tobyte (stream.
ReadByte ());
/////encoding//unicode {0xFF, 0xFE} based on the first 4 bytes of file stream;
Be-unicode {0xFE, 0xFF};
UTF8 = {0xEF, 0xBB, 0xBF};
if (byte1 = = 0xFE && byte2 = 0xFF)//unicodebe {targetencoding = Encoding.bigendianunicode; } if (byte1 = 0xFF && byte2 = 0xFE && byte3!= 0xFF)//unicode {targetencoding = Encoding.
Unicode; } if (byte1 = 0xEF && byte2 = 0xBB && byte3 = 0xBF)//utf8 {targetencoding = Encoding.utf
8; //restore seek location stream.
Seek (Origpos, seekorigin.begin);
return targetencoding; //New Add a method to solve the UTF8 coding problem without BOM///<summary>///the encoding type of the file is judged by the given file flow///</summary>/// ;p Aram Name= "FS" > File Flow </param>///<returns> file encoding type </returns> public static System.Text.Encoding getencoding (Stream FS) {
byte[] Unicode = new byte[] {0xFF, 0xFE, 0x41};
byte[] Unicodebig = new byte[] {0xFE, 0xFF, 0x00}; byte[] UTF8 = new byte[] {0xEF, 0xBB, 0xBF};
With BOM Encoding reVal = Encoding.default;
BinaryReader r = new BinaryReader (FS, System.Text.Encoding.Default);
byte[] ss = R.readbytes (4);
if (ss[0] = = 0xFE && ss[1] = = 0xFF && ss[2] = = 0x00) {reVal = Encoding.bigendianunicode;
else if (ss[0] = = 0xFF && ss[1] = = 0xFE && ss[2] = = 0x41) {reVal = Encoding.unicode;
else {if (ss[0] = = 0xEF && ss[1] = = 0xBB && ss[2] = 0xBF) {reVal = Encoding.UTF8;
else {int i; Int. TryParse (fs.
Length.tostring (), out i);
SS = R.readbytes (i);
if (Isutf8bytes (ss)) ReVal = Encoding.UTF8;
} r.close ();
return reVal;
} <summary>///to determine if it is UTF8 format without BOM///</summary>///<param name= "Data" ></param> <returns></returns> private static bool Isutf8bytes (byte[] Data {int charbytecounter = 1; Calculates the number of bytes that are currently being parsed Fu Ching byte Curbyte;
The byte of the current analysis. for (int i = 0; i < data. Length;
i++) {curbyte = Data[i]; if (Charbytecounter = = 1) {if (Curbyte >= 0x80) {//To determine the current while ((Curbyte <<= 1) ;
0x80)!= 0) {charbytecounter++; }//Mark bit first if not 0 then at least 2 1 start as: 110XXXXX ...
1111110X if (charbytecounter = 1 | | charbytecounter > 6) {return false;
}} else {//if UTF-8 at this time the first digit must be 1 if ((Curbyte & 0xc0)!= 0x80) {return false;
} charbytecounter--;
} if (Charbytecounter > 1) {throw new Exception ("Not expected byte format!");
return true; }
}
Summarize
The above is the C # Automatic identification file encoding all the content, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.