根据字节数组的前四字节判断字符编码
luyued 发布于 2011-01-19 06:16 浏览 N 次string filename = "C://TEST.TXT";
string str = "ab中国";
public void test(System.Text.Encoding encod)
{
System.IO.File.AppendAllText(filename, str, encod);
byte[] arrBytes = File.ReadAllBytes(filename);//实际获取中不用传递整个数组,前面4字节即可
System.IO.File.Delete(filename);
txtContent.Text += (encod.ToString()+" >"+encod.WebName + ">>"+ ReturnEncoding(arrBytes).WebName) + System.Environment.NewLine;
}
///
/// 根据字节数组的前四字节判断字符编码
///
///
///
public static System.Text.Encoding ReturnEncoding(byte[] tB)
{
byte tB1 = 0;
byte tB2 = 0;
byte tB3 = 0;
byte tB4 = 0;
if (tB.Length <2)
return null;
tB1 = tB[0];
tB2 = tB[1];
if (tB.Length >= 3) tB3 = tB[2];
if (tB.Length >= 4) tB4 = tB[3];
if ((tB1 == 0xfe &&tB2 == 0xff)) return System.Text.Encoding.BigEndianUnicode;
if ((tB1 == 0xff &&tB2 == 0xfe &&tB3 != 0xff)) return System.Text.Encoding.Unicode;
if ((tB1 == 0xef &&tB2 == 0xbb &&tB3 == 0xbf)) return System.Text.Encoding.UTF8;
return System.Text.Encoding.Default;
}
//
System.Text.ASCIIEncoding >us-ascii >>gb2312
System.Text.UnicodeEncoding >unicodeFFFE >>unicodeFFFE
System.Text.DBCSCodePageEncoding >gb2312 >>gb2312
System.Text.UnicodeEncoding >utf-16 >>utf-16
System.Text.UTF32Encoding >utf-32 >>utf-16
System.Text.UTF7Encoding >utf-7 >>gb2312
System.Text.UTF8Encoding >utf-8 >>utf-8
ab中国
ascii 97 98 63 63 (不能识别'中国')
big 254 2550 97 0 98 78 45 86 253
gb2312 97 98 214 208 185 250 (default)
unicode 255 254 97 0 98 0 45 78 253 86
utf32 255 254 0 0 97 0 0 0 98 0 0 0 45 78 00 253 86 00
utf7 97 98 43 84 105 49 87 47 81 45
utf8 239 187 191 97 98 228 184 173 229 155 189
类别:c# 查看评论
上一篇:最具潜质男模宋鹏举代言TB2.JEANS男装 下一篇:《领袖风采TB2》
相关资讯
- 07-01· 禁教唐诗算术能还幼儿快
- 07-01· 2011年06月17日
- 07-01· 唐诗宋词英译:李商隐 筹
- 07-01· 仿评《唐诗1000首》第186首
- 07-01· 没事干的时候背背唐诗吧
- 07-01· [转载]唐诗中“斜”字该读
- 07-01· 湖南醴陵瓷业转型升级
- 07-01· 奇瑞风云2两厢黑色|2010款
- 07-01· 摩根士丹利华鑫摩根士丹
- 07-01· 摩根士丹利华鑫近期优选
图文资讯
最新资讯
- 07-01· 中金投行部大摩出售中金
- 07-01· 摩根士丹利招聘6月2日【实
- 07-01· 营养防病圣典
- 07-01· 《博伽梵歌原意》之第十
- 07-01· [不错]斑斓圣典---减肥中常
- 07-01· 武乐圣典《太极武当》:武
- 07-01· 铁血英雄-现阶段战功牌兑
- 07-01· 2011年06月10日【原创】南歌
- 07-01· 【淘宝网信息】- 2010年的
- 07-01· 深圳品牌女装有哪些?