天天看点

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

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

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

把UTF-8编码转换为GB2312编码
< script type = " text/javascript " >
把UTF-8编码转换为GB2312编码
<!--
把UTF-8编码转换为GB2312编码
csdn_AD_Position_GroupID = " {f05ff3bf-246b-4d71-a101-b5d4ee3f6cd3} " ;
把UTF-8编码转换为GB2312编码
csdn_AD_Page_Url = document.location;
把UTF-8编码转换为GB2312编码
// -->
把UTF-8编码转换为GB2312编码
</ script >
把UTF-8编码转换为GB2312编码
< script type = " text/javascript " src = " http://ads.csdn.net/AD/Show_JavaScript_AD.js " >
把UTF-8编码转换为GB2312编码
</ script >

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

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

把UTF-8编码转换为GB2312编码
< globalization requestEncoding ="utf-8" responseEncoding ="utf-8" />

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

把UTF-8编码转换为GB2312编码
Encoding gb2312 = Encoding.GetEncoding( " gb2312 " );
把UTF-8编码转换为GB2312编码
Response.ContentEncoding = gb2312;

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

把UTF-8编码转换为GB2312编码
string utfinfo = " document.write(/"alert('aa你好么??');/"); " ;
把UTF-8编码转换为GB2312编码
string gb2312info = string .Empty;
把UTF-8编码转换为GB2312编码
把UTF-8编码转换为GB2312编码
Encoding utf8 = Encoding.UTF8;
把UTF-8编码转换为GB2312编码
Encoding gb2312 = Encoding.GetEncoding( " gb2312 " );
把UTF-8编码转换为GB2312编码
把UTF-8编码转换为GB2312编码
// Convert the string into a byte[].
把UTF-8编码转换为GB2312编码
byte [] unicodeBytes = utf8.GetBytes(utfinfo);
把UTF-8编码转换为GB2312编码
// Perform the conversion from one encoding to the other.
把UTF-8编码转换为GB2312编码
byte [] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);
把UTF-8编码转换为GB2312编码
把UTF-8编码转换为GB2312编码
// Convert the new byte[] into a char[] and then into a string.
把UTF-8编码转换为GB2312编码
// This is a slightly different approach to converting to illustrate
把UTF-8编码转换为GB2312编码
// the use of GetCharCount/GetChars.
把UTF-8编码转换为GB2312编码
char [] asciiChars = new char [gb2312.GetCharCount(asciiBytes, 0 , asciiBytes.Length)];
把UTF-8编码转换为GB2312编码
gb2312.GetChars(asciiBytes, 0 , asciiBytes.Length, asciiChars, 0 );
把UTF-8编码转换为GB2312编码
gb2312info = new string (asciiChars);
把UTF-8编码转换为GB2312编码

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

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

反馈

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

#  re: 把UTF-8编码转换为GB2312编码 怎麼我登陸csdn社區老出現"未將對象引用設置到對象的實例"錯誤信息?

2005/5/19 16:52 | bill

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

#  re: 把UTF-8编码转换为GB2312编码 我要新版论坛...不要广告系统... 2005/5/20 1:32 | sunmast

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

#  re: 把UTF-8编码转换为GB2312编码 to: bill

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

另外清除本地 Cookie 和 临时文件看看

2005/5/20 8:46 | ghj1976

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

#  re: 把UTF-8编码转换为GB2312编码 怎麼我登陸csdn社區老出現"未將對象引用設置到對象的實例"錯誤信息?

2005/5/25 9:14 | CrazyTiger

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

#  re: 把UTF-8编码转换为GB2312编码 怎麼我登陸csdn社區老出現"未將對象引用設置到對象的實例"錯誤信息?

2005/5/25 9:21 | CrazyTiger

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

#  re:把UTF-8编码转换为GB2312编码 把UTF-8编码转换为GB2312编码ooeess 2005/6/7 15:07 | 皮带张力计

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

#  把UTF-8编码转换为GB2312编码 Ping Back来自:blog.csdn.net 2005/6/18 18:11 | Super

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

#  re: 把UTF-8编码转换为GB2312编码 想请教一个问题:

如何将一个utf-8编码的页面内的表单填写的内容,送到gb2312的页面上去不出现乱码.

楼主写的在非ASP.net 应用,我不是很明白.请回复!

谢谢!

2005/7/20 15:15 | packsy

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

#  re: 把UTF-8编码转换为GB2312编码 谢谢提示,但还是不知道怎么运用! 2005/9/23 10:56 | sunny

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

#  re: 把UTF-8编码转换为GB2312编码 以上只是修改整体的默认编码,如果只有某个页的编码需要修改,ASP.net 中则可以简单的使用下面代码:

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

Response.ContentEncoding = gb2312;

具体放在什么位置呀。 2005/10/29 16:23 | thr

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

#  把UTF-8编码转换为GB2312编码 把UTF-8编码转换为GB2312编码 2005/12/13 12:38 | seraph5186

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

#  re: 把UTF-8编码转换为GB2312编码 以上只是修改整体的默认编码,如果只有某个页的编码需要修改,ASP.net 中则可以简单的使用下面代码:

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

Response.ContentEncoding = gb2312;

具体放在什么位置呀。(Page_Load里即可)

2006/3/1 10:20 | 无名

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

#  re: 把UTF-8编码转换为GB2312编码 在采集页面的时候有什么办法程序当前页面用的是什么编码 2006/6/10 18:49 | qq

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

#  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 | 大调动

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

#  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 | 大调动

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

#  re: 把UTF-8编码转换为GB2312编码 你好,在网上看到你的一篇文章,得知你正在写广告系统.想请教你几点:

.net程序怎么样结合js实现"那个时间显示那个广告,显示几次,或者那些广告组合"

望指教,不慎感激!!!

我的邮箱是:[email protected] 2006/8/8 16:14 | keb

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

#  把UTF-8编码转换为GB2312编码<转> 把UTF-8编码转换为GB2312编码 2005年5月19日 10:44 --> 最近在做的广告系统中,碰到了一个问题,广告系统采用的UTF-8编码,而一些使用这套广告系统的频道页面使用的是GB2312编码。当然也有使用UTF-8编码的频道使用这套广告系统。 2006/9/2 17:53 | dongdong

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

#  回复: 把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

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

#  回复: 把UTF-8编码转换为GB2312编码 vb.net↑ 2006/11/30 18:13 | kkk

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

#  回复: 把UTF-8编码转换为GB2312编码 ????? 2006/12/13 21:15 | de

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

#  回复: 把UTF-8编码转换为GB2312编码 你好,在网上看到你的一篇文章,得知你正在写广告系统.想请教你几点:

.net程序怎么样结合js实现"那个时间显示那个广告,显示几次,或者那些广告组合"

望指教,不慎感激!!!

我的邮箱是:[email protected] 2006/12/29 9:00 | 我考百试通

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

#  回复: 把UTF-8编码转换为GB2312编码 VB6中有完美解决UFT-8的转换么?

中英文混合的如: "我们要2000年的DATA资料" 2007/1/19 1:40 | ZYYUJQ

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

#  回复: 把UTF-8编码转换为GB2312编码 VB6中有完美解决UFT-8的转换么?

中英文混合的如: "我们要2000年的DATA资料"

ZYYUJQ#126.COM

2007/1/19 1:41 | ZYYUJQ

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

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

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

2007/2/1 22:56 | Rui

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

#  回复: 把UTF-8编码转换为GB2312编码 灏婃暚鐨凱engxiao Hao

鎮ㄥソ锛侀潪甯告劅璋㈡偍鐨勬潵淇★紝鎴戝緢楂樺叴鑳芥湁鏈轰細甯姪鎮ㄣ€?

鎴戜滑寤鸿鎮ㄦ妸鍦≒aypal涓婁慨鏀规偍鐨勯摱琛屼俊鎭紝鍥犱负Paypal鏃犳硶鍚戦摱琛屽嚭绀轰换浣曡瘉鏄庢枃浠躲€?

鏀瑰彉閾惰璐︽埛璁剧疆鐨勮繃绋嬪涓嬶細

1锛夌櫥闄嗘偍鐨凱ayPal璐︽埛

2锛夎繘鍏モ€滅敤鎴蜂俊鎭€?3锛夌偣鍑烩€滈摱琛岃处鎴封€?4锛夐€夋嫨閿欒鐨勯摱琛屽笎鍙凤紝鐐光€滅Щ闄も€?5)鐐光€滄坊鍔犫€濓紝閲嶆柊娣诲姞姝g‘鐨勯摱琛岃处鎴枫€?

璇峰師璋呯粰鎮ㄥ甫鏉ョ殑璇稿涓嶄究銆傚鏋滄偍杩樻湁浠讳綍鐨勭枒闂紝璇峰洖澶嶆閭欢锛屼笌鎴戜滑鑱旂郴銆傞潪甯告劅璋㈡偍瀵筆ayPal涓€濡傛棦寰€鐨勬敮鎸併€?

Jovine

PayPal鍏ㄧ悆瀹㈡埗鏈嶅姟閮?

2007/7/25 13:27 | 123

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

#  回复: 把UTF-8编码转换为GB2312编码 没看明白 2007/12/31 11:37 | 威客部落

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

#  回复: 把UTF-8编码转换为GB2312编码 原来这样,学习了。 2007/12/31 11:37 | 威客部落

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

#  回复: 把UTF-8编码转换为GB2312编码 原来这样,学习了。.……………………………………………………………………………………………………………………………………。