资源简介
有时我们不知道接收到的字节流是何种编码的字节流,当转换成字符串的时候也就不能正确转换。例如,实现pop3协议时,我接收到的网络比特流不知是utf-8还是gb2312,解码邮件时就可能会出现乱码。而此代码是判断接收到的字节流是何种编码形式。

代码片段和文件信息
using System;
namespace IdentifyEncoding.Text
{
#region Class IdentifyEncoding.....
///
/// 检测字符编码的类
///
///
///
///
///
/// /// IdentifyEncoding 用来检测 字节数组的编码.
/// Create By lion
/// 2005-02-21 22:00
/// Support .Net framework v1.1.4322
/// WebSite:www.lionsky.net(lion-a AT sohu.com)
/// ]]>
///
public class IdentifyEncoding
{
#region Fields.....
// Frequency tables to hold the GB Big5 and EUC-TW character
// frequencies
internal static int[][] GBFreq = new int[94][];
internal static int[][] GBKFreq = new int[126][];
internal static int[][] Big5Freq = new int[94][];
internal static int[][] EUC_TWFreq = new int[94][];
internal static string[] nicename = new string[]
{
“GB2312“ “GBK“ “HZ“ “Big5“ “CNS 11643“
“ISO 2022CN“ “UTF-8“ “Unicode“ “ASCII“ “OTHER“
};
#endregion
#region Methods.....
///
/// 初始化 的实例
///
public IdentifyEncoding()
{
Initialize_Frequencies();
}
#region GetEncodingName.....
///
/// 从指定的 中判断编码类型
///
/// 要判断的
/// 返回编码类型(“GB2312“ “GBK“ “HZ“ “Big5“ “CNS 11643“ “ISO 2022CN“ “UTF-8“ “Unicode“ “ASCII“ “OTHER“)
///
/// 以下示例演示了如何调用 方法:
///
/// IdentifyEncoding ide = new IdentifyEncoding();
/// Response.Write(ide.GetEncodingName(new Uri(“http://china5.nikkeibp.co.jp/china/news/com/200307/pr_com200307170131.html“)));
///
///
public virtual string GetEncodingName(System.Uri testurl)
{
byte[] rawtext = new byte[1024];
int bytesread = 0 byteoffset = 0;
System.IO.Stream chinesestream;
try
{
chinesestream = System.Net.WebRequest.Create(testurl.AbsoluteUri).GetResponse().GetResponseStream();
while ((bytesread = ReadInput(chinesestream ref rawtext byteoffset rawtext.Length - byteoffset)) > 0)
{
byteoffset += bytesread;
}
chinesestream.Close();
}
catch (System.Exception e)
{
System.Console.Error.WriteLine(“Error loading or using URL “ + e.ToString());
}
return GetEncodingName
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 98936 2008-10-24 12:21 IdentifyEncoding.cs
----------- --------- ---------- ----- ----
98936 1
相关资源
- WPF USB 网络 串口 通信软件
- B/S 网上订餐系统
- 教室管理系统.rar
- UNICODE GBK双向码表二进制文件
- 小鸡快跑游戏.
-
分别适用于.NET fr
amework 2.0和4.0的E - 汽车租赁系统............................
- 德卡D8读写器关于读写感应卡的一些代
- halcon 测量助手
- 图片存储到数据库保存二进制文件并
- 全自动多功能编码转换工具(URLASCI
- 用Socket写的简易FTP服务器和客户端
- 企业销售管理信息系统(全套)
- 串口操作类(justinio)
- 基于Petri网的工作流(完整的原创源代
- 选择题自动考试系统
- 多线程实例:桌面智能弹球小游戏
- 土地信息管理系统
- ServiceStack V3.9 全部dll
- PDF pdfview.ocx 无水印
- 程序源代码行数统计工具
- TH upstream-inhibited ARHGAP12 subnetwork for
- 无需共享打印机实现远程打印功能小
- 真正的破解版PDFView4NET
- 网页调用ActiveX控件获取串口数据
- 哈雷NMX网管及ProStream-1000操作手册V6
- spark streaming技术内幕与源码剖析
- Luence的与盘古分词的使用软件
- Emgu.CV 打开视频与人脸检测
- zhwiki-20200720-pages-articles-multistream5.xm
评论
共有 条评论