如何在西文ISO88591页面上正确输入存储显示中文。

这篇文章是测试如何将西文页面上显示中文的,将Web.Config文件中的globalization节点修改为西文设置,

<globalization culture="en-US" requestEncoding="iso-8859-1" responseEncoding="iso-8859-1" />

可以在页面显示编码为西文,那又如何能在此西文页面上显示存储中文信息呢,
建立ASPX页面进行测试:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UnicodeToGB2312.aspx.cs" Inherits="UnicodeToGB2312" %>

<!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>Untitled Page</title>
    
<script language="javascript" type="text/javascript">

        
function encode(obj)
        
{
            alert(
0);
            
var hidTxt = document.getElementById("hidTxtContent");
            hidTxt.value
=obj.value.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"$2;")});
            
var aa = hidTxt.value.split("&");
            
var bb = "";
            
for(var i=1; i<aa.length; i++)
            
{
                bb 
= bb + "&amp;" + aa[i];
            }

           
// hidTxt.value=hidTxt.value.replace("&", "&amp;");
           hidTxt.value=bb;
            alert(hidTxt.value);
        }

        
        
function unencode(obj)
        
{
            obj.value
=unescape(obj.value.replace(//g,'%u').replace(/;/g,''));
        }

        
        
function setTextValueFromHidden()
        
{
            
var text = document.getElementById("txtContent");
            
var lblValue = document.getElementById("lblValue");
            text.value 
= lblValue.innerText;
            lblValue.style.display 
= "none";
        }

    
</script>
</head>
<body onload="setTextValueFromHidden()">
    
<form id="form1" runat="server">
        
<input id="hidTxtContent" type="hidden" name="hidTxtContent" runat="server">
        
<asp:Label ID="lblValue" runat="server" Text="Label" ></asp:Label><div style="text-align: center">
        
&nbsp;
        
<asp:TextBox ID="txtContent" runat="server" Height="262px" TextMode="MultiLine" Width="492px"></asp:TextBox>
        
<br />
        
<br />
        
<asp:Button ID="btnConvert" runat="server" Text="escape" OnClick="btnConvert_Click" Height="24px" Width="59px" />
            
<br />
            
<br />
            
<br />
            
<asp:Button ID="Button1" runat="server" Text="Button" /></div>

    
</form>
</body>
</html>

后台代码:
using System;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
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;

public partial class UnicodeToGB2312 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
this.btnConvert.Attributes.Add("onclick""encode(" + this.txtContent.ID + ");");
    }


    
protected void btnConvert_Click(object sender, EventArgs e)
    
{
        
string result = txtContent.Text;
        result 
= this.hidTxtContent.Value;
        result 
= result.Replace("&amp;""&");
        result 
= UnEncode(result);
        result 
= Encode(result);
        
this.lblValue.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, @"([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 
= "" + 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(02), System.Globalization.NumberStyles.AllowHexSpecifier);
        bytes[
0= byte.Parse(result.Substring(22), 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"
        }

    }
 

}


这样就可以完成此功能。
如下为源代码:
WebUnicodeTest
原文地址:https://www.cnblogs.com/adam/p/1056310.html