实现unicode与gb2312编码转换!

本文介绍了一种使用C#进行Unicode与GB2312编码相互转换的方法。通过正则表达式及自定义处理函数实现了字符串编码转换的功能。此技术适用于需要在不同字符集间进行数据交换的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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;


public partial class UnicodeToGB2312 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void btnConvert_Click(object sender, EventArgs e)
    
{
        
string result = txtContent.Text;
        
if (btnConvert.Text == "解码")
        
{
            btnConvert.Text 
= "编码";
            result 
= UnEncode(result);
            txtContent.Text 
= result;
        }

        
else
        
{
            btnConvert.Text 
= "解码";
            result 
= Encode(result);
            txtContent.Text 
= result;           
        }

    }

    
protected string Encode(string inputString)
    
{
        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 %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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">
        
&nbsp;
        
<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值