登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

樱之花

叶散的时候,你明白欢聚;花谢的时候,你明白青春.

 
 
 

日志

 
 
关于我

分类中“我的实验室”是我在日常工作中的一些知识总结,有些写的比较匆忙,可能大家在阅读时会产生困扰,后期有时间我会重新整理编辑,谢谢大家的到访,您们的支持是我前进的动力!

把UTF-8编码转换为GB2312编码   

2007-08-08 10:42:44|  分类: .NET/C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

不同编码的页面、脚本之间互相引用,就会产生乱码的问题,解决方法就是统一成一种编码。

asp.net 中,如果要修改输出页面的编码,可以通过修改web.config中以下配置信息

<globalization requestEncoding="utf-8" responseEncoding="utf-8" />

以上只是修改整体的默认编码,如果只有某个页的编码需要修改,ASP.net 中则可以简单的使用下面代码:

Encoding gb2312 = Encoding.GetEncoding("gb2312");Response.ContentEncoding = gb2312;

在非ASP.net 应用中,可能你读到的数据是UTF-8编码,但是你要转换为GB2312编码,则可以参考以下代码:

string utfinfo = "document.write(\"alert('aa你好么??');\");";

string gb2312info = string.Empty;

Encoding utf8 = Encoding.UTF8;

Encoding gb2312 = Encoding.GetEncoding("gb2312");

// Convert the string into a byte[].

byte[] unicodeBytes = utf8.GetBytes(utfinfo);

// Perform the conversion from one encoding to the other.

byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);

// Convert the new byte[] into a char[] and then into a string.

// This is a slightly different approach to converting to illustrate

// the use of GetCharCount/GetChars.

char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];

gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);

gb2312info = new string(asciiChars);

当然,其他各种编码之间的转换,跟上述代码也类似的,就不描述了。

  评论这张
 
阅读(748)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018