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

最近在做的广告系统中,碰到了一个问题,广告系统采用的UTF-8编码,而一些使用这套广告系统的频道页面使用的是GB2312编码。当然也有使用UTF-8编码的频道使用这套广告系统。

频道页面是通过嵌入类似如下的代码方式,来调用广告的。具体那个时间显示那个广告,或者那些广告组合是广告系统自己处理的。

 
 
< script type = " text/javascript " > <!-- csdn_AD_Position_GroupID = " {f05ff3bf-246b-4d71-a101-b5d4ee3f6cd3} " ; csdn_AD_Page_Url = document.location; // --> </ script > < script type = " text/javascript " src = " http://ads.csdn.net/AD/Show_JavaScript_AD.js " > </ script >


不同编码的页面、脚本之间互相引用,就会产生乱码的问题,解决方法就是统一成一种编码。
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);

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

打印 | posted on 2005年5月19日 10:44 | Filed Under [ .net 编程心得 ] | 收藏本页 (百度搜藏)(QQ书签)(Live收藏)(Google书签)(Yahoo书签)(新浪ViVi)(搜狐网摘)(365Key网摘)(天极网摘)(博采网摘)(和讯网摘)

反馈

Gravatar
#  re: 把UTF-8编码转换为GB2312编码
怎麼我登陸csdn社區老出現"未將對象引用設置到對象的實例"錯誤信息?
2005/5/19 16:52 | bill
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
我要新版论坛...不要广告系统...
2005/5/20 1:32 | sunmast
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
to: bill

那个页面?? 不同的页面,可能会是不同的原因。

另外清除本地 Cookie 和 临时文件看看
2005/5/20 8:46 | ghj1976
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
怎麼我登陸csdn社區老出現"未將對象引用設置到對象的實例"錯誤信息?
2005/5/25 9:14 | CrazyTiger
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
怎麼我登陸csdn社區老出現"未將對象引用設置到對象的實例"錯誤信息?
2005/5/25 9:21 | CrazyTiger
Gravatar
#  re:把UTF-8编码转换为GB2312编码
把UTF-8编码转换为GB2312编码ooeess
2005/6/7 15:07 | 皮带张力计
Gravatar
#  把UTF-8编码转换为GB2312编码
Ping Back来自:blog.csdn.net
2005/6/18 18:11 | Super
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
想请教一个问题:
如何将一个utf-8编码的页面内的表单填写的内容,送到gb2312的页面上去不出现乱码.
楼主写的在非ASP.net 应用,我不是很明白.请回复!

谢谢!
2005/7/20 15:15 | packsy
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
谢谢提示,但还是不知道怎么运用!
2005/9/23 10:56 | sunny
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
以上只是修改整体的默认编码,如果只有某个页的编码需要修改,ASP.net 中则可以简单的使用下面代码:

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

具体放在什么位置呀。
2005/10/29 16:23 | thr
Gravatar
#  把UTF-8编码转换为GB2312编码
把UTF-8编码转换为GB2312编码
2005/12/13 12:38 | seraph5186
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
以上只是修改整体的默认编码,如果只有某个页的编码需要修改,ASP.net 中则可以简单的使用下面代码:

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

具体放在什么位置呀。(Page_Load里即可)
2006/3/1 10:20 | 无名
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
在采集页面的时候有什么办法程序当前页面用的是什么编码
2006/6/10 18:49 | qq
Gravatar
#  re: 把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);
2006/7/18 0:14 | 大调动
Gravatar
#  re: 把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);
2006/7/18 0:14 | 大调动
Gravatar
#  re: 把UTF-8编码转换为GB2312编码
你好,在网上看到你的一篇文章,得知你正在写广告系统.想请教你几点:
.net程序怎么样结合js实现"那个时间显示那个广告,显示几次,或者那些广告组合"
望指教,不慎感激!!!
我的邮箱是:utmostkeb@gmail.com
2006/8/8 16:14 | keb
Gravatar
#  把UTF-8编码转换为GB2312编码<转>
把UTF-8编码转换为GB2312编码 2005年5月19日 10:44 --> 最近在做的广告系统中,碰到了一个问题,广告系统采用的UTF-8编码,而一些使用这套广告系统的频道页面使用的是GB2312编码。当然也有使用UTF-8编码的频道使用这套广告系统。
2006/9/2 17:53 | dongdong
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)]
-->
char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1]
2006/11/30 18:04 | kkk
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
vb.net↑
2006/11/30 18:13 | kkk
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
?????
2006/12/13 21:15 | de
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
你好,在网上看到你的一篇文章,得知你正在写广告系统.想请教你几点:
.net程序怎么样结合js实现"那个时间显示那个广告,显示几次,或者那些广告组合"
望指教,不慎感激!!!
我的邮箱是:utmostkeb@gmail.com
2006/12/29 9:00 | 我考百试通
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
VB6中有完美解决UFT-8的转换么?
中英文混合的如: "我们要2000年的DATA资料"
2007/1/19 1:40 | ZYYUJQ
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
VB6中有完美解决UFT-8的转换么?
中英文混合的如: "我们要2000年的DATA资料"

ZYYUJQ#126.COM
2007/1/19 1:41 | ZYYUJQ
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
很有趣的結果, 我用上面的方法转换UTF8 TO GB2312 OR BIG5, 结果是当中文显示在网页时,无论你选择哪一种ENCODING,E。G WESTENEUROPEAN ,中文都可以正确的显示,无论是简体或繁体。

这真是太好了, 但是为什末?


2007/2/1 22:56 | Rui
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
灏婃暚鐨凱engxiao Hao

鎮ㄥソ锛侀潪甯告劅璋㈡偍鐨勬潵淇★紝鎴戝緢楂樺叴鑳芥湁鏈轰細甯姪鎮ㄣ€?
鎴戜滑寤鸿鎮ㄦ妸鍦≒aypal涓婁慨鏀规偍鐨勯摱琛屼俊鎭紝鍥犱负Paypal鏃犳硶鍚戦摱琛屽嚭绀轰换浣曡瘉鏄庢枃浠躲€?
鏀瑰彉閾惰璐︽埛璁剧疆鐨勮繃绋嬪涓嬶細
1锛夌櫥闄嗘偍鐨凱ayPal璐︽埛
2锛夎繘鍏モ€滅敤鎴蜂俊鎭€?3锛夌偣鍑烩€滈摱琛岃处鎴封€?4锛夐€夋嫨閿欒鐨勯摱琛屽笎鍙凤紝鐐光€滅Щ闄も€?5)鐐光€滄坊鍔犫€濓紝閲嶆柊娣诲姞姝g‘鐨勯摱琛岃处鎴枫€?

璇峰師璋呯粰鎮ㄥ甫鏉ョ殑璇稿涓嶄究銆傚鏋滄偍杩樻湁浠讳綍鐨勭枒闂紝璇峰洖澶嶆閭欢锛屼笌鎴戜滑鑱旂郴銆傞潪甯告劅璋㈡偍瀵筆ayPal涓€濡傛棦寰€鐨勬敮鎸併€?
Jovine
PayPal鍏ㄧ悆瀹㈡埗鏈嶅姟閮?
2007/7/25 13:27 | 123
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
没看明白
2007/12/31 11:37 | 威客部落
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
原来这样,学习了。
2007/12/31 11:37 | 威客部落
Gravatar
#  回复: 把UTF-8编码转换为GB2312编码
原来这样,学习了。.……………………………………………………………………………………………………………………………………。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值