C#判斷檔案編碼——常用字法
阿新 • • 發佈:2018-11-11
使用中文寫文章,當篇幅超過一定程度,必然會使用到諸如:“的”、“你”、“我”這樣的常用字。本類思想便是提取中文最常用的一百個字,使用中文世界常用編碼(主要有GBK、GB2312、GB18030、UTF-8、UTF-32、Unicode、BigEndianUnicode及UTF-7等)獲得其編碼位元組,以其為搜尋詞到目標流進行查詢,如果查詢得到則表示該流使用此種編碼。顯而易見此類不適用於小篇幅。
using System.Collections.Generic; using System.IO; using System.Text; namespace YunShenBuZhiChu.MiMaBenJiaMiFa {/// <summary> /// 文字編碼檢測。 /// 用於檢測一篇文章使用什麼編碼方式進行編碼。 /// </summary> public class StreamBianMaJianCe { /// <summary> /// BigEndianUnicode編碼高頻漢字編碼 /// </summary> private List<byte[]> _BigEndianUnicodeGaoPinZiFuBianMaLsit = new List<byte[]>() {new byte[2]{118,132},new byte[2]{78,0},new byte[2]{86,253},new byte[2]{87,40},new byte[2]{78,186},new byte[2]{78,134},new byte[2]{103,9},new byte[2]{78,45}, new byte[2]{102,47},new byte[2]{94,116},new byte[2]{84,140},new byte[2]{89,39},new byte[2]{78,26},new byte[2]{78,13},new byte[2]{78,58},newbyte[2]{83,209}, new byte[2]{79,26},new byte[2]{93,229},new byte[2]{126,207},new byte[2]{78,10},new byte[2]{87,48},new byte[2]{94,2},new byte[2]{137,129},new byte[2]{78,42}, new byte[2]{78,167},new byte[2]{143,217},new byte[2]{81,250},new byte[2]{136,76},new byte[2]{79,92},new byte[2]{117,31},new byte[2]{91,182},new byte[2]{78,229}, new byte[2]{98,16},new byte[2]{82,48},new byte[2]{101,229},new byte[2]{108,17},new byte[2]{103,101},new byte[2]{98,17},new byte[2]{144,232},new byte[2]{91,249}, new byte[2]{143,219},new byte[2]{89,26},new byte[2]{81,104},new byte[2]{94,250},new byte[2]{78,214},new byte[2]{81,108},new byte[2]{95,0},new byte[2]{78,236}, new byte[2]{87,58},new byte[2]{92,85},new byte[2]{101,246},new byte[2]{116,6},new byte[2]{101,176},new byte[2]{101,185},new byte[2]{78,59},new byte[2]{79,1}, new byte[2]{141,68},new byte[2]{91,158},new byte[2]{91,102},new byte[2]{98,165},new byte[2]{82,54},new byte[2]{101,63},new byte[2]{109,78},new byte[2]{117,40}, new byte[2]{84,12},new byte[2]{78,142},new byte[2]{108,213},new byte[2]{154,216},new byte[2]{149,127},new byte[2]{115,176},new byte[2]{103,44},new byte[2]{103,8}, new byte[2]{91,154},new byte[2]{83,22},new byte[2]{82,160},new byte[2]{82,168},new byte[2]{84,8},new byte[2]{84,193},new byte[2]{145,205},new byte[2]{81,115}, new byte[2]{103,58},new byte[2]{82,6},new byte[2]{82,155},new byte[2]{129,234},new byte[2]{89,22},new byte[2]{128,5},new byte[2]{83,58},new byte[2]{128,253}, new byte[2]{139,190},new byte[2]{84,14},new byte[2]{92,49},new byte[2]{123,73},new byte[2]{79,83},new byte[2]{78,11},new byte[2]{78,7},new byte[2]{81,67}, new byte[2]{121,62},new byte[2]{143,199},new byte[2]{82,77},new byte[2]{151,98},new byte[2]{48,2},new byte[2]{255,12},new byte[2]{255,31},new byte[2]{255,1} }; /// <summary> /// UTF8編碼高頻漢字編碼 /// </summary> private List<byte[]> _UTF8GaoPinZiFuBianMaLsit = new List<byte[]>() { new byte[3]{231,154,132},new byte[3]{228,184,128},new byte[3]{229,155,189},new byte[3]{229,156,168},new byte[3]{228,186,186},new byte[3]{228,186,134}, new byte[3]{230,156,137},new byte[3]{228,184,173},new byte[3]{230,152,175},new byte[3]{229,185,180},new byte[3]{229,146,140},new byte[3]{229,164,167}, new byte[3]{228,184,154},new byte[3]{228,184,141},new byte[3]{228,184,186},new byte[3]{229,143,145},new byte[3]{228,188,154},new byte[3]{229,183,165}, new byte[3]{231,187,143},new byte[3]{228,184,138},new byte[3]{229,156,176},new byte[3]{229,184,130},new byte[3]{232,166,129},new byte[3]{228,184,170}, new byte[3]{228,186,167},new byte[3]{232,191,153},new byte[3]{229,135,186},new byte[3]{232,161,140},new byte[3]{228,189,156},new byte[3]{231,148,159}, new byte[3]{229,174,182},new byte[3]{228,187,165},new byte[3]{230,136,144},new byte[3]{229,136,176},new byte[3]{230,151,165},new byte[3]{230,176,145}, new byte[3]{230,157,165},new byte[3]{230,136,145},new byte[3]{233,131,168},new byte[3]{229,175,185},new byte[3]{232,191,155},new byte[3]{229,164,154}, new byte[3]{229,133,168},new byte[3]{229,187,186},new byte[3]{228,187,150},new byte[3]{229,133,172},new byte[3]{229,188,128},new byte[3]{228,187,172}, new byte[3]{229,156,186},new byte[3]{229,177,149},new byte[3]{230,151,182},new byte[3]{231,144,134},new byte[3]{230,150,176},new byte[3]{230,150,185}, new byte[3]{228,184,187},new byte[3]{228,188,129},new byte[3]{232,181,132},new byte[3]{229,174,158},new byte[3]{229,173,166},new byte[3]{230,138,165}, new byte[3]{229,136,182},new byte[3]{230,148,191},new byte[3]{230,181,142},new byte[3]{231,148,168},new byte[3]{229,144,140},new byte[3]{228,186,142}, new byte[3]{230,179,149},new byte[3]{233,171,152},new byte[3]{233,149,191},new byte[3]{231,142,176},new byte[3]{230,156,172},new byte[3]{230,156,136}, new byte[3]{229,174,154},new byte[3]{229,140,150},new byte[3]{229,138,160},new byte[3]{229,138,168},new byte[3]{229,144,136},new byte[3]{229,147,129}, new byte[3]{233,135,141},new byte[3]{229,133,179},new byte[3]{230,156,186},new byte[3]{229,136,134},new byte[3]{229,138,155},new byte[3]{232,135,170}, new byte[3]{229,164,150},new byte[3]{232,128,133},new byte[3]{229,140,186},new byte[3]{232,131,189},new byte[3]{232,174,190},new byte[3]{229,144,142}, new byte[3]{229,176,177},new byte[3]{231,173,137},new byte[3]{228,189,147},new byte[3]{228,184,139},new byte[3]{228,184,135},new byte[3]{229,133,131}, new byte[3]{231,164,190},new byte[3]{232,191,135},new byte[3]{229,137,141},new byte[3]{233,157,162},new byte[3]{227,128,130},new byte[3]{239,188,140}, new byte[3]{239,188,159},new byte[3]{239,188,129}, }; /// <summary> /// Unicode編碼高頻漢字編碼 /// </summary> private List<byte[]> _UnicodeGaoPinZiFuBianMaLsit = new List<byte[]>() { new byte[2]{132,118},new byte[2]{0,78},new byte[2]{253,86},new byte[2]{40,87},new byte[2]{186,78},new byte[2]{134,78},new byte[2]{9,103},new byte[2]{45,78}, new byte[2]{47,102},new byte[2]{116,94},new byte[2]{140,84},new byte[2]{39,89},new byte[2]{26,78},new byte[2]{13,78},new byte[2]{58,78},new byte[2]{209,83}, new byte[2]{26,79},new byte[2]{229,93},new byte[2]{207,126},new byte[2]{10,78},new byte[2]{48,87},new byte[2]{2,94},new byte[2]{129,137},new byte[2]{42,78}, new byte[2]{167,78},new byte[2]{217,143},new byte[2]{250,81},new byte[2]{76,136},new byte[2]{92,79},new byte[2]{31,117},new byte[2]{182,91},new byte[2]{229,78}, new byte[2]{16,98},new byte[2]{48,82},new byte[2]{229,101},new byte[2]{17,108},new byte[2]{101,103},new byte[2]{17,98},new byte[2]{232,144},new byte[2]{249,91}, new byte[2]{219,143},new byte[2]{26,89},new byte[2]{104,81},new byte[2]{250,94},new byte[2]{214,78},new byte[2]{108,81},new byte[2]{0,95},new byte[2]{236,78}, new byte[2]{58,87},new byte[2]{85,92},new byte[2]{246,101},new byte[2]{6,116},new byte[2]{176,101},new byte[2]{185,101},new byte[2]{59,78},new byte[2]{1,79}, new byte[2]{68,141},new byte[2]{158,91},new byte[2]{102,91},new byte[2]{165,98},new byte[2]{54,82},new byte[2]{63,101},new byte[2]{78,109},new byte[2]{40,117}, new byte[2]{12,84},new byte[2]{142,78},new byte[2]{213,108},new byte[2]{216,154},new byte[2]{127,149},new byte[2]{176,115},new byte[2]{44,103},new byte[2]{8,103}, new byte[2]{154,91},new byte[2]{22,83},new byte[2]{160,82},new byte[2]{168,82},new byte[2]{8,84},new byte[2]{193,84},new byte[2]{205,145},new byte[2]{115,81}, new byte[2]{58,103},new byte[2]{6,82},new byte[2]{155,82},new byte[2]{234,129},new byte[2]{22,89},new byte[2]{5,128},new byte[2]{58,83},new byte[2]{253,128}, new byte[2]{190,139},new byte[2]{14,84},new byte[2]{49,92},new byte[2]{73,123},new byte[2]{83,79},new byte[2]{11,78},new byte[2]{7,78},new byte[2]{67,81}, new byte[2]{62,121},new byte[2]{199,143},new byte[2]{77,82},new byte[2]{98,151},new byte[2]{2,48},new byte[2]{12,255},new byte[2]{31,255},new byte[2]{1,255} }; /// <summary> /// UTF32編碼高頻漢字編碼 /// </summary> private List<byte[]> _UTF32GaoPinZiFuBianMaLsit = new List<byte[]>() { new byte[4]{132,118,0,0},new byte[4]{0,78,0,0},new byte[4]{253,86,0,0},new byte[4]{40,87,0,0},new byte[4]{186,78,0,0},new byte[4]{134,78,0,0},new byte[4]{9,103,0,0}, new byte[4]{45,78,0,0},new byte[4]{47,102,0,0},new byte[4]{116,94,0,0},new byte[4]{140,84,0,0},new byte[4]{39,89,0,0},new byte[4]{26,78,0,0},new byte[4]{13,78,0,0}, new byte[4]{58,78,0,0},new byte[4]{209,83,0,0},new byte[4]{26,79,0,0},new byte[4]{229,93,0,0},new byte[4]{207,126,0,0},new byte[4]{10,78,0,0},new byte[4]{48,87,0,0}, new byte[4]{2,94,0,0},new byte[4]{129,137,0,0},new byte[4]{42,78,0,0},new byte[4]{167,78,0,0},new byte[4]{217,143,0,0},new byte[4]{250,81,0,0},new byte[4]{76,136,0,0}, new byte[4]{92,79,0,0},new byte[4]{31,117,0,0},new byte[4]{182,91,0,0},new byte[4]{229,78,0,0},new byte[4]{16,98,0,0},new byte[4]{48,82,0,0},new byte[4]{229,101,0,0}, new byte[4]{17,108,0,0},new byte[4]{101,103,0,0},new byte[4]{17,98,0,0},new byte[4]{232,144,0,0},new byte[4]{249,91,0,0},new byte[4]{219,143,0,0},new byte[4]{26,89,0,0}, new byte[4]{104,81,0,0},new byte[4]{250,94,0,0},new byte[4]{214,78,0,0},new byte[4]{108,81,0,0},new byte[4]{0,95,0,0},new byte[4]{236,78,0,0},new byte[4]{58,87,0,0}, new byte[4]{85,92,0,0},new byte[4]{246,101,0,0},new byte[4]{6,116,0,0},new byte[4]{176,101,0,0},new byte[4]{185,101,0,0},new byte[4]{59,78,0,0},new byte[4]{1,79,0,0}, new byte[4]{68,141,0,0},new byte[4]{158,91,0,0},new byte[4]{102,91,0,0},new byte[4]{165,98,0,0},new byte[4]{54,82,0,0},new byte[4]{63,101,0,0},new byte[4]{78,109,0,0}, new byte[4]{40,117,0,0},new byte[4]{12,84,0,0},new byte[4]{142,78,0,0},new byte[4]{213,108,0,0},new byte[4]{216,154,0,0},new byte[4]{127,149,0,0},new byte[4]{176,115,0,0}, new byte[4]{44,103,0,0},new byte[4]{8,103,0,0},new byte[4]{154,91,0,0},new byte[4]{22,83,0,0},new byte[4]{160,82,0,0},new byte[4]{168,82,0,0},new byte[4]{8,84,0,0}, new byte[4]{193,84,0,0},new byte[4]{205,145,0,0},new byte[4]{115,81,0,0},new byte[4]{58,103,0,0},new byte[4]{6,82,0,0},new byte[4]{155,82,0,0},new byte[4]{234,129,0,0}, new byte[4]{22,89,0,0},new byte[4]{5,128,0,0},new byte[4]{58,83,0,0},new byte[4]{253,128,0,0},new byte[4]{190,139,0,0},new byte[4]{14,84,0,0},new byte[4]{49,92,0,0}, new byte[4]{73,123,0,0},new byte[4]{83,79,0,0},new byte[4]{11,78,0,0},new byte[4]{7,78,0,0},new byte[4]{67,81,0,0},new byte[4]{62,121,0,0},new byte[4]{199,143,0,0}, new byte[4]{77,82,0,0},new byte[4]{98,151,0,0},new byte[4]{2,48,0,0},new byte[4]{12,255,0,0},new byte[4]{31,255,0,0},new byte[4]{1,255,0,0} }; /// <summary> /// UTF7編碼高頻漢字編碼 /// </summary> private List<byte[]> _UTF7GaoPinZiFuBianMaLsit = new List<byte[]>() { new byte[5]{43,100,111,81,45},new byte[5]{43,84,103,65,45},new byte[5]{43,86,118,48,45},new byte[5]{43,86,121,103,45},new byte[5]{43,84,114,111,45},new byte[5]{43,84,111,89,45}, new byte[5]{43,90,119,107,45},new byte[5]{43,84,105,48,45},new byte[5]{43,90,105,56,45},new byte[5]{43,88,110,81,45},new byte[5]{43,86,73,119,45},new byte[5]{43,87,83,99,45}, new byte[5]{43,84,104,111,45},new byte[5]{43,84,103,48,45},new byte[5]{43,84,106,111,45},new byte[5]{43,85,57,69,45},new byte[5]{43,84,120,111,45},new byte[5]{43,88,101,85,45}, new byte[5]{43,102,115,56,45},new byte[5]{43,84,103,111,45},new byte[5]{43,86,122,65,45},new byte[5]{43,88,103,73,45},new byte[5]{43,105,89,69,45},new byte[5]{43,84,105,111,45}, new byte[5]{43,84,113,99,45},new byte[5]{43,106,57,107,45},new byte[5]{43,85,102,111,45},new byte[5]{43,105,69,119,45},new byte[5]{43,84,49,119,45},new byte[5]{43,100,82,56,45}, new byte[5]{43,87,55,89,45},new byte[5]{43,84,117,85,45},new byte[5]{43,89,104,65,45},new byte[5]{43,85,106,65,45},new byte[5]{43,90,101,85,45},new byte[5]{43,98,66,69,45}, new byte[5]{43,90,50,85,45},new byte[5]{43,89,104,69,45},new byte[5]{43,107,79,103,45},new byte[5]{43,87,47,107,45},new byte[5]{43,106,57,115,45},new byte[5]{43,87,82,111,45}, new byte[5]{43,85,87,103,45},new byte[5]{43,88,118,111,45},new byte[5]{43,84,116,89,45},new byte[5]{43,85,87,119,45},new byte[5]{43,88,119,65,45},new byte[5]{43,84,117,119,45}, new byte[5]{43,86,122,111,45},new byte[5]{43,88,70,85,45},new byte[5]{43,90,102,89,45},new byte[5]{43,100,65,89,45},new byte[5]{43,90,98,65,45},new byte[5]{43,90,98,107,45}, new byte[5]{43,84,106,115,45},new byte[5]{43,84,119,69,45},new byte[5]{43,106,85,81,45},new byte[5]{43,87,53,52,45},new byte[5]{43,87,50,89,45},new byte[5]{43,89,113,85,45}, new byte[5]{43,85,106,89,45},new byte[5]{43,90,84,56,45},new byte[5]{43,98,85,52,45},new byte[5]{43,100,83,103,45},new byte[5]{43,86,65,119,45},new byte[5]{43,84,111,52,45}, new byte[5]{43,98,78,85,45},new byte[5]{43,109,116,103,45},new byte[5]{43,108,88,56,45},new byte[5]{43,99,55,65,45},new byte[5]{43,90,121,119,45},new byte[5]{43,90,119,103,45}, new byte[5]{43,87,53,111,45},new byte[5]{43,85,120,89,45},new byte[5]{43,85,113,65,45},new byte[5]{43,85,113,103,45},new byte[5]{43,86,65,103,45},new byte[5]{43,86,77,69,45}, new byte[5]{43,107,99,48,45},new byte[5]{43,85,88,77,45},new byte[5]{43,90,122,111,45},new byte[5]{43,85,103,89,45},new byte[5]{43,85,112,115,45},new byte[5]{43,103,101,111,45}, new byte[5]{43,87,82,89,45},new byte[5]{43,103,65,85,45},new byte[5]{43,85,122,111,45},new byte[5]{43,103,80,48,45},new byte[5]{43,105,55,52,45},new byte[5]{43,86,65,52,45}, new byte[5]{43,88,68,69,45},new byte[5]{43,101,48,107,45},new byte[5]{43,84,49,77,45},new byte[5]{43,84,103,115,45},new byte[5]{43,84,103,99,45},new byte[5]{43,85,85,77,45}, new byte[5]{43,101,84,52,45},new byte[5]{43,106,56,99,45},new byte[5]{43,85,107,48,45},new byte[5]{43,108,50,73,45},new byte[5]{43,77,65,73,45},new byte[5]{43,47,119,119,45}, new byte[5]{43,47,120,56,45},new byte[5]{43,47,119,69,45} }; /// <summary> /// GB18030編碼高頻漢字編碼 /// </summary> private List<byte[]> _GB18030GaoPinZiFuBianMaLsit = new List<byte[]>() { new byte[2]{181,196},new byte[2]{210,187},new byte[2]{185,250},new byte[2]{212,218},new byte[2]{200,203},new byte[2]{193,203},new byte[2]{211,208},new byte[2]{214,208},new byte[2]{202,199}, new byte[2]{196,234},new byte[2]{186,205},new byte[2]{180,243},new byte[2]{210,181},new byte[2]{178,187},new byte[2]{206,170},new byte[2]{183,162},new byte[2]{187,225},new byte[2]{185,164}, new byte[2]{190,173},new byte[2]{201,207},new byte[2]{181,216},new byte[2]{202,208},new byte[2]{210,170},new byte[2]{184,246},new byte[2]{178,250},new byte[2]{213,226},new byte[2]{179,246}, new byte[2]{208,208},new byte[2]{215,247},new byte[2]{201,250},new byte[2]{188,210},new byte[2]{210,212},new byte[2]{179,201},new byte[2]{181,189},new byte[2]{200,213},new byte[2]{195,241}, new byte[2]{192,180},new byte[2]{206,210},new byte[2]{178,191},new byte[2]{182,212},new byte[2]{189,248},new byte[2]{182,224},new byte[2]{200,171},new byte[2]{189,168},new byte[2]{203,251}, new byte[2]{185,171},new byte[2]{191,170},new byte[2]{195,199},new byte[2]{179,161},new byte[2]{213,185},new byte[2]{202,177},new byte[2]{192,237},new byte[2]{208,194},new byte[2]{183,189}, new byte[2]{214,247},new byte[2]{198,243},new byte[2]{215,202},new byte[2]{202,181},new byte[2]{209,167},new byte[2]{177,168},new byte[2]{214,198},new byte[2]{213,254},new byte[2]{188,195}, new byte[2]{211,195},new byte[2]{205,172},new byte[2]{211,218},new byte[2]{183,168},new byte[2]{<