File character stream encoding judgment

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Text;
Using System.Web;

Namespace Wordsearch1
{
public class fileencoding
{
Public fileencoding ()
{
//
TODO: Add constructor logic here
//
}

<summary>
Gets the encoding of a text file. If a valid leader cannot be found at the head of the file, 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>
Gets the encoding of a 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>
//Gets the encoding of a text file.
//</summary>
//<param name= "filename" > file name. </param>
//<param name= "defaultencoding" > Default encoding method. This encoding is returned when the method cannot get a valid leader from the head 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>
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 get a valid leader from the head of the file. </param>
<returns></returns>
public static Encoding GetEncoding (FileStream stream, Encoding defaultencoding)
{
Encoding targetencoding = defaultencoding;
if (stream! = null && stream. Length >= 2)
{
Save the first 4 bytes of a file stream
byte byte1 = 0;
byte Byte2 = 0;
byte byte3 = 0;
byte byte4 = 0;
Save 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 ());
}

Judging encoding based on the first 4 bytes of the file stream
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;
}

Restore Seek position
Stream. Seek (Origpos, Seekorigin.begin);
}
return targetencoding;
}
}
}

File character stream encoding judgment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.