---
title: "中文与韩、日文混排出现在Gb2312编码的Aspx的处理方法"
author: "hecal"
date: "2006-12-30T10:29:00Z"
tags: []
slug: "gb2312-aspx"
draft: false
lastmod: "2011-12-07T18:10:48Z"
id: 1296557
url: "https://hecal.iteye.com/blog/1296557"
---

有一个Web项目，Web.Config中requestEncoding和responseEncoding都是gb2312，而从数据库中取出的简介数据可能是中文和韩、日文混合的内容，这时候如果直接输出到页面上，其页面会出现乱码，其中的韩文内容无法正确显示。当然如果项目的编码都使用Utf-8的话将没有这个问题，但这个项目是一个老项目，为了尽量不要影响已有的程序，所以无法将编码改为Utf-8,只能在本页面上动脑筋。

经过研究，发现这个问题可以通过Html实体的方法解决。

对于Html实体请参考：

[Character entity references in HTML 4](http://www.w3.org/TR/REC-html40/sgml/entities.html "Character entity references in HTML 4")

[HTML Document Representation](http://www.w3.org/TR/REC-html40/charset.html "Character entity references in HTML 4")

测试代码：

Byte\[] bComments = Encoding.UTF8.GetBytes("一ンブル????中文");  
char\[] cComments = Encoding.UTF8.GetChars(bComments);

StringBuilder charBuilder = new StringBuilder();  
foreach(char c in cComments)  
{  
if(c &gt; '/u0800')  
{  
charBuilder.Append("&#");  
charBuilder.Append((int)c);  
}  
else  
{  
charBuilder.Append(c);  
}  
}  
Response.Write(charBuilder.ToString());

这段代码的作用是将所有的中文、韩文、日文字符通过硬编码输出成为html实体。而Html实体是不受ResponseEncoding和页面编码集影响的。

说明：

/u0800 以上的为中、韩、日字符。

中文的范围：/u4e00 - /u9fa5，日文在/u0800 - /u4e00，韩文为/u9fa5以上。