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.