在JAVASCRIPT中可以用encode和unencode函数再用上正则表达式可以将比如"中" 转化成"中"表示.
在C#中我们可以用Rexgex.Repalce()这个方法来处理,实现的代码如下:
页面的代码如下:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Text;
using
System.Text.RegularExpressions;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
partial
class
UnicodeToGB2312 : System.Web.UI.Page
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
protected void Page_Load(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
protected void btnConvert_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string result = txtContent.Text;
if (btnConvert.Text == "解码")
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
btnConvert.Text = "编码";
result = UnEncode(result);
txtContent.Text = result;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
btnConvert.Text = "解码";
result = Encode(result);
txtContent.Text = result;
}
}
protected string Encode(string inputString)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MatchEvaluator matchEvaluator = new MatchEvaluator(EncodeFindSubString); //初始化一个委托,该委托用于处理Regex.Repalce中每次匹配的match对象
string result = System.Text.RegularExpressions.Regex.Replace(inputString, @"[^/u0000-/u00FF]", matchEvaluator);
return result;
}
protected string UnEncode(string inputString)
{
MatchEvaluator matchEvaluator = new MatchEvaluator(UnEncodeFindSubString);
string result = System.Text.RegularExpressions.Regex.Replace(inputString, @"&#x([0-9a-fA-F]{4});", matchEvaluator);
return result;
}
protected string EncodeFindSubString(Match match) //编码的时候委托处理函数
{
byte[] bytes = Encoding.Unicode.GetBytes(match.Value);
string result = "";
for (int i = bytes.Length - 1; i >= 0; i--)
{
result += ToHexString(bytes[i]);
}
result = "&#x" + result + ";";
return result;
}
protected string UnEncodeFindSubString(Match match) //解码的时候委托处理函数
{
string result = match.Groups[1].Value;
byte[] bytes = new byte[2]; //4E 2D
bytes[1] = byte.Parse(result.Substring(0,2),System.Globalization.NumberStyles.AllowHexSpecifier);
bytes[0] = byte.Parse(result.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
result = result.Replace(result, Encoding.Unicode.GetString(bytes));
return result;
}
protected string ToHexString(byte a) //返回一个16进制表示的数
{
string result = a.ToString("X");
if (result.Length == 2)
{
return result;
}
else
{
return "0" + result; //如果就一位比如"7"的16进制返回的是"7"而我们需要 "07"
}
}
}
<%
...
@ Page Language="C#" AutoEventWireup="true" CodeFile="UnicodeToGB2312.aspx.cs" Inherits="UnicodeToGB2312" validateRequest=false
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
Unicode
<
-
>
GB2312转换
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
style
="text-align: center"
>
<
asp:TextBox
ID
="txtContent"
runat
="server"
Height
="507px"
TextMode
="MultiLine"
Width
="814px"
></
asp:TextBox
>
<
br
/>
<
br
/>
<
asp:Button
ID
="btnConvert"
runat
="server"
Text
="解码"
OnClick
="btnConvert_Click"
Height
="24px"
Width
="59px"
/></
div
>
</
form
>
</
body
>
</
html
>