国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Asp.net把UTF-8編碼轉換為GB2312編碼

2019-11-18 17:26:06
字體:
來源:轉載
供稿:網友

最近在做的系統中,碰到了一個問題,交易系統采用的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);

當然,其他各種編碼之間的轉換,跟上述代碼也類似的,就不描述了。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开远市| 赣州市| 莱西市| 广德县| 绥江县| 邢台县| 柯坪县| 如皋市| 鱼台县| 永寿县| 丹凤县| 富宁县| 漳平市| 南和县| 滨州市| 渝北区| 济南市| 淮南市| 英超| 新干县| 会同县| 格尔木市| 和平县| 库车县| 旅游| 砚山县| 佛坪县| 改则县| 中阳县| 探索| 九龙坡区| 西峡县| 依兰县| 葵青区| 乐清市| 西宁市| 万山特区| 平泉县| 平泉县| 久治县| 简阳市|