.net里的汉字编码
做了个例子来说明
前台
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="获取简体中文编码"></asp:Button>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:Button id="Button2" runat="server" Text="获取繁体中文编码"></asp:Button>后台
private void Button1_Click(object sender, System.EventArgs e)
{
//获取GB2312编码页(表)
Encoding gb=Encoding.GetEncoding("gb2312"); 
byte[] bytes=gb.GetBytes(this.TextBox1.Text.Trim()) ;
string lowCode = System.Convert.ToString(bytes[0],16); //取出元素1编码内容(两位16进制)
string hightCode = System.Convert.ToString(bytes[1],16);//取出元素2编码内容(两位16进制)
this.Response.Write(lowCode + hightCode);

//重新将编码组合成文字
byte byte1=Convert.ToByte(lowCode,16);
byte byte2=Convert.ToByte(hightCode,16); 
//将两个字节变量存储在字节数组中
byte[] str_r=new byte[]{byte1,byte2}; 
//根据汉字编码的字节数组解码出中文汉字
string str1=gb.GetString((byte[])Convert.ChangeType(str_r, typeof(byte[]))); 
this.Response.Write(str1);
}
private void Button2_Click(object sender, System.EventArgs e)
{
Encoding big=Encoding.GetEncoding(950);//936是GB2312编码,950是Big5编码
byte[] bytes = big.GetBytes(this.TextBox2.Text.Trim());
string lowCode = System.Convert.ToString(bytes[0],16); //取出元素1编码内容(两位16进制)
string hightCode = System.Convert.ToString(bytes[1],16);//取出元素2编码内容(两位16进制)
this.Response.Write(lowCode + hightCode);
//重新将编码组合成文字
//将两个字节变量存储在字节数组中
byte[] str_r=new byte[]{bytes[0],bytes[1]};
//根据汉字编码的字节数组解码出中文汉字
string str1=big.GetString((byte[])Convert.ChangeType(str_r, typeof(byte[]))); 
this.Response.Write(str1);
}


浙公网安备 33010602011771号