The solution method of reading Chinese file garbled in C #

Source: Internet
Author: User
FileStream afile = new FileStream (Singlefile, FileMode.Open); StreamReader sr = new StreamReader (Afile, encoding.getencoding ("gb2312"), true); string filecontent = Sr. ReadToEnd (); Afile.close (); ProcessData Pd = new ProcessData (); Pd.procedata (filecontent);

StreamReader using 3 parameters The last automatic detection utf-8, Chinese is mostly gb2312, if not utf-8, use gb2312


The system comes with UTF detection, see the following

private void detectencoding () {if (This.bytelen >= 2) {this._detectencoding = false;        BOOL flag = FALSE; if ((this.bytebuffer[0] = = 0xFE) && (this.bytebuffer[1] = = 0xff)) {this.encoding = new Unicodee            Ncoding (True, true);            This.compressbuffer (2);        Flag = true; } else if ((this.bytebuffer[0] = = 0xff) && (this.bytebuffer[1] = = 0xFE)) {if ((this.byt ELen < 4) | | (this.bytebuffer[2]! = 0)) ||            (this.bytebuffer[3]! = 0))                {this.encoding = new UnicodeEncoding (false, True);                This.compressbuffer (2);            Flag = true;                } else {this.encoding = new UTF32Encoding (false, True);                This.compressbuffer (4);            Flag = true; }} else if (((This.bytelen >= 3) && (this.bytebuffer[0] = = 0xEF)) && ((this.bytebuffer[1 ] = = 0XBB)&& (this.bytebuffer[2] = = 0XBF)) {this.encoding = Encoding.UTF8;            This.compressbuffer (3);        Flag = true; } else if ((((This.bytelen >= 4) && (this.bytebuffer[0] = = 0)) && ((this.bytebuffer[1] = = 0) &am p;& (this.bytebuffer[2] = = 0xFE)) && (this.bytebuffer[3] = = 0xff)) {this.encoding = new UT            F32encoding (True, true);            This.compressbuffer (4);        Flag = true;        } else if (This.bytelen = = 2) {this._detectencoding = true;            } if (flag) {This.decoder = This.encoding.GetDecoder ();            This._maxcharsperbuffer = This.encoding.GetMaxCharCount (this.byteBuffer.Length);        This.charbuffer = new Char[this._maxcharsperbuffer]; }    }}
  • 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.