惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
B
Blog RSS Feed
小众软件
小众软件
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 聂微东
博客园_首页
B
Blog
雷峰网
雷峰网
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
D
Docker
博客园 - 司徒正美
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
U
Unit 42
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
腾讯CDC
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
Jina AI
Jina AI
WordPress大学
WordPress大学
D
DataBreaches.Net
V
V2EX
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
The Hacker News
The Hacker News
Security Archives - TechRepublic
Security Archives - TechRepublic
IT之家
IT之家
P
Privacy International News Feed

博客园 - 老枪

sql 修改时间 接近完美的CSS标题、列表溢出隐藏 - 老枪 - 博客园 通过js读取或设置FCKeditor中的值 - 老枪 - 博客园 access查找表是否存在 css 如何设置 某div 里面的所有图片的最大宽度。当加入文章里图片时,图片自动按比例减小。 高素质图片压缩 检索COM类工厂中CLSID...”的两个错误的解决方法 检索COM类工厂中CLSID...”的两个错误的解决方法 111111 Lucene基于Java的全文检索引擎简介 sql2005 中文分词 - 老枪 - 博客园 gridview使用大全 ASP.NET程序中常用的三十三种代码 - 老枪 - 博客园 使用System.Web.Mail通过需验证的邮件服务器发送邮件 - 老枪 - 博客园 40种网页常用小技巧(javascript) - 老枪 - 博客园 ASP.NET替换HTML代码<>中的任意代码 - 老枪 - 博客园 SQL SERVER 2005 同步复制技术 网页制作之在线视频播放代码 - 老枪 - 博客园 常用javascript数据验 - 老枪 - 博客园 asp.net2.0中,使用gridview显示新闻标题
Asp.net把UTF-8编码转换为GB2312编码
老枪 · 2009-12-21 · via 博客园 - 老枪

Asp.net把UTF-8编码转换为GB2312编码

最近在做的系统中,碰到了一个问题,交易系统采用的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);

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


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ITFLY8/archive/2006/03/23/633263.aspx