How does C automatically recognize the encoding of a file
- 2021-11-14 06:43:49
- OfStack
Preface
The code of the identification file in C # is a headache. Recently, when importing the background refund data of WeChat merchants, no matter how to set the code, it was garbled. Later, I found the code of the identification file code on the Internet and felt good. Finally, it was identified as gb2312. It seems that I am still too dregs, so I can only eat dirt, and I forgot this code.
The following words do not say much, on the code.
/// <summary>
/// Used to obtain 1 How to encode a text file (Encoding) .
/// </summary>
public class TxtFileEncoder
{
public TxtFileEncoder()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// Acquire 1 The encoding method of text files. If a valid preamble cannot be found in the file header, Encoding.Default Will be returned.
/// </summary>
/// <param name="fileName"> File name. </param>
/// <returns></returns>
public static Encoding GetEncoding(string fileName)
{
return GetEncoding(fileName, Encoding.Default);
}
/// <summary>
/// Acquire 1 Encoding method of text file stream.
/// </summary>
/// <param name="stream"> Text file stream. </param>
/// <returns></returns>
public static Encoding GetEncoding(FileStream stream)
{
return GetEncoding(stream, Encoding.Default);
}
/// <summary>
/// Acquire 1 The encoding method of text files.
/// </summary>
/// <param name="fileName"> File name. </param>
/// <param name="defaultEncoding"> Default encoding method. This encoding is returned when the method cannot get a valid preamble from the header of the file. </param>
/// <returns></returns>
public static Encoding GetEncoding(string fileName, Encoding defaultEncoding)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
Encoding targetEncoding = GetEncoding(fs, defaultEncoding);
fs.Close();
return targetEncoding;
}
/// <summary>
/// Acquire 1 Encoding method of 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 get a valid preamble from the header of the file. </param>
/// <returns></returns>
public static Encoding GetEncoding(FileStream stream, Encoding defaultEncoding)
{
Encoding targetEncoding = defaultEncoding;
if (stream != null && stream.Length >= 2)
{
// Before saving the file stream 4 Bytes
byte byte1 = 0;
byte byte2 = 0;
byte byte3 = 0;
byte byte4 = 0;
// Save the current Seek Position
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());
}
// According to the preamble of the file stream 4 Byte judgment Encoding
//Unicode {0xFF, 0xFE};
//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.UTF8;
}
// Recovery Seek Position
stream.Seek(origPos, SeekOrigin.Begin);
}
return targetEncoding;
}
// New additions 1 A method to solve the problem without BOM Adj. UTF8 Coding problem
/// <summary>
/// The encoding type of the file is judged by the given file stream
/// </summary>
/// <param name="fs"> File stream </param>
/// <returns> Encoding type of file </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 }; // Belt 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>
/// Judge whether it is not BOM Adj. UTF8 Format
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private static bool IsUTF8Bytes(byte[] data)
{
int charByteCounter = 1; // Calculates the number of additional bytes of the character currently being parsed
byte curByte; // Bytes currently parsed .
for (int i = 0; i < data.Length; i++)
{
curByte = data[i];
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
// Judge the current
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
// If the first bit of the tag bit is not 0 Then at least with 2 A 1 Begin Such as :110XXXXX...........1111110X
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
// If UTF-8 At this time 1 Bit must be 1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception(" Unexpected byte Format !");
}
return true;
}
}
Summarize
The above is C # automatic identification of all the contents of file coding, I hope that the content of this article can bring 1 certain help to your study or work, if you have any questions, you can leave a message for communication.