编程联盟 编程联盟
广告联系 站点地图 设为首页
站长作品 关于本站 收藏本站
首页>>开发资料>>.NET编程>> asp.net把utf-8编码转换为gb2312编码代码

asp.net把utf-8编码转换为gb2312编码代码

查看次数: 1028
发布时间:2008-4-10 0:00:00
作者: 编程联盟
来源: 编程联盟
 

最近在做的系统中,碰到了一个问题,交易系统采用的utf-8编码,而一些支持系统使用的是gb2312编码。

不同编码的页面、脚本之间互相引用,就会产生乱码的问题,解决方法就是统一成一种编码。
asp.net 中,如果要修改输出页面的编码,可以通过修改web.config中以下配置信息


<globalization requestencoding="utf-8" responseencoding="utf-8" />
以上只是修改整体的默认编码,如果只有某个页的编码需要修改,asp.net 中则可以简单的使用下面代码:


注:加到page_load()事件下面就可以了
encoding gb2312 = encoding.getencoding("gb2312");
response.contentencoding = gb2312;
在非asp.net 应用中,可能你读到的数据是utf-8编码,但是你要转换为gb2312编码,则可以参考以下代码:

string utfinfo = "document.write(\"alert('你好么??');\");";
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);

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


-
文章来源:http://www.bcbbs.net/news/Content.aspx?id=39
文章来源:http://www.bcbbs.net/news/Content.aspx?id=39
收藏本文章 关闭本页
精彩推荐
精彩推荐
编程联盟 | 编程论坛 | 关于本站 | 站点地图 | 友情链接 | 联系我们

Powered by 编程联盟版权所有 ? 2007-2009 bcbbs.net
冀ICP备07500001号 冀ICP备08005404号


空间赞助商:北京易德科技有限公司