UTF8编码和其它编码之间的转化
来源:互联网 发布:淘宝网店代理靠谱么 编辑:程序博客网 时间:2024/04/30 01:17
本文的例子里的其它编码使用的是“西里尔文编码”,可以把“Windows-1251 ”改成你需要的编码,第一个函数是西里尔文编码转化成UTF8编码,第二个函数是UTF8编码转化成西里尔文编码,欢迎大家来交流。
/// <summary> /// 西里尔文编码转化成UTF8编码 /// </summary> public void XiLiErToUTF8() { Encoding xilier = Encoding.GetEncoding("Windows-1251"); Encoding utf8 = Encoding.UTF8; StreamReader sr = new StreamReader(Server.MapPath("1doneW.txt"), xilier); string xilierinfo = sr.ReadToEnd(); byte[] xilierBytes = xilier.GetBytes(xilierinfo); byte[] utf8Bytes = Encoding.Convert(xilier, utf8, xilierBytes); int len = utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length); char[] utf8Chars = new char[len]; utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0); string utf8info = new string(utf8Chars); FileInfo file = new FileInfo(Server.MapPath("1doneW_utf8.txt")); if (file.Exists) { file.Delete(); } using (StreamWriter sw = new StreamWriter(file.FullName, true, utf8)) { sw.WriteLine(utf8info, Encoding.UTF8); Response.Write("西里尔文编码转化成UTF8编码已完成<br/>"); } }
/// <summary> /// UTF8编码转化成西里尔文编码 /// </summary> public void UTF8ToXiLiEr() { Encoding utf8 = Encoding.UTF8; Encoding xilier = Encoding.GetEncoding("Windows-1251"); StreamReader sr = new StreamReader(Server.MapPath("1doneU.txt"), utf8); string utf8info = sr.ReadToEnd(); byte[] utf8Bytes = utf8.GetBytes(utf8info); byte[] xilierBytes = Encoding.Convert(utf8, xilier, utf8Bytes); int len = xilier.GetCharCount(xilierBytes, 0, xilierBytes.Length); char[] xilierChars = new char[len]; xilier.GetChars(xilierBytes, 0, xilierBytes.Length, xilierChars, 0); string xilierinfo = new string(xilierChars); FileInfo file = new FileInfo(Server.MapPath("1doneU_xilier.txt")); if (file.Exists) { file.Delete(); } using (StreamWriter sw = new StreamWriter(file.FullName, true, xilier)) { sw.WriteLine(xilierinfo); Response.Write("UTF8编码转化成西里尔文编码已完成<br/>"); } }
1doneW.txt里面放置的是西里尔文编码的文字
1doneU.txt里面放置的是UTF8编码的文字
附件:源代码下载
- UTF8编码和其它编码之间的转化
- 其它编码的字符转utf8编码
- GB2312和BIG5,Unicode/UTF8等编码之间的互相转化
- Swift中String和UTF8编码的NSData相互转化
- GBK和UTF8编码之间互相转换的两个函数
- 简单演示字节和字符之间的编码转化
- utf8编码转化为unicode的一个精彩函数
- base64-utf8编码的NSString转化为NSString
- UTF8和GB编码的识别
- unicode和utf8编码的互相转换
- utf8和unicode编码的关系
- 【编码格式】GBK和UTF8 的区别
- utf8的编码算法
- utf8的编码算法
- utf8的编码原理
- UTF8的编码解码
- GBK与UTF8 编码转化(C++)
- 数据库编码格式转化(gb2312->utf8)
- 批量执行sql文件并输出执行日志
- 简化函数调用之十五 :Replace Exception with Test(以测试取代异常)
- 2010-03-31 09:56 Java中修改系统时间
- 伪造 X-Forwarded-For
- layer-list XML和LayerDrawable实例
- UTF8编码和其它编码之间的转化
- 电梯调度算法
- 查询所有订单的最新信息的SQL语句的写法 DISTINCT配合count和group by一起完成
- error LNK2019: 无法解析的外部符号
- shell中:=的作用
- html 页面 不能,,,
- 计算机字符编码问题总析(基于Python)
- finereport怎么和web开发结合
- css 超出自动隐藏