C#簡略斷定字符編碼的辦法。本站提示廣大學習愛好者:(C#簡略斷定字符編碼的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#簡略斷定字符編碼的辦法正文
本文實例講述了C#簡略斷定字符編碼的辦法。分享給年夜家供年夜家參考,詳細以下:
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 strReslut = Encoding.UTF8.GetString(buff); } else if (buff[0] == 254 && buff[1] == 255) {// big endian unicode strReslut = Encoding.BigEndianUnicode.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 isUtf8(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] & 0x80) != 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; }
願望本文所述對年夜家C#法式設計有所贊助。