C # Simple method of judging character encoding _c# tutorial

Source: Internet
Author: User

The example in this article describes the method of C # simple judgment character encoding. Share to everyone for your reference, specific as follows:

public static string GetText (byte[] buff) {string strreslut = string.
  Empty; if (buff. Length > 3) {if (buff[0] = 239 && buff[1] = = 187 && Buff[2] = = 191) {//Utf-8 Strresl
    UT = Encoding.UTF8.GetString (buff); else if (buff[0] = = 254 && buff[1] = = 255) {//big endian unicode Strreslut = Encoding.bigendianun Icode.
    GetString (Buff); else if (buff[0] = = 255 && buff[1] = = 254) {//Unicode Strreslut = Encoding.Unicode.GetString (buff
    );
    else if (IsUtf8 (buff)) {//Utf-8 Strreslut = Encoding.UTF8.GetString (buff);
    else {//ANSI Strreslut = Encoding.Default.GetString (buff);
} return Strreslut; //110XXXXX, 10XXXXXX//1110XXXX, 10XXXXXX, 10XXXXXX//11110XXX, 10XXXXXX, 10XXXXXX, 10XXXXXX private static bool Isut F8 (byte[] buff) {for (int i = 0; i < buff. Length; i++) {if (Buff[i] & 0xe0 = = 0xc0)//110x xxxx 10xx xxxx {if (BUFf[i + 1] & 0x80)!= 0x80) {return false; else if ((Buff[i] & 0xF0) = = 0xe0)//1110 xxxx 10xx xxxx 10xx xxxx {if (buff[i + 1] & 0x8 0)!= 0x80 | |
      (Buff[i + 2] & 0x80)!= 0x80) {return false;  else if ((Buff[i] & 0xf8) = = 0xF0)//1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx {if (buff[i + 1] & 0x80)!= 0x80 | | (Buff[i + 2] & 0x80)!= 0x80 | |
      (Buff[i + 3] & 0x80)!= 0x80) {return false;
}} return true;

 }//news.sohu.com private static bool ISGBK (byte[] buff) {return false;}

Read more about C # Interested readers can view the site topics: "C # Coding Skills Summary", "C # XML file Operation skills Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # An introductory course on object-oriented programming and a summary of thread usage tips for C # programming

I hope this article will help you with C # programming.

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.