.net里的汉字编码

做了个例子来说明
前台

None.gif < asp:TextBox  id ="TextBox1"  runat ="server" ></ asp:TextBox >
None.gif
< asp:Button  id ="Button1"  runat ="server"  Text ="获取简体中文编码" ></ asp:Button >
None.gif
< asp:TextBox  id ="TextBox2"  runat ="server" ></ asp:TextBox >
None.gif
< asp:Button  id ="Button2"  runat ="server"  Text ="获取繁体中文编码" ></ asp:Button >

后台
None.gif private   void  Button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//获取GB2312编码页(表) 
InBlock.gif
            Encoding gb=Encoding.GetEncoding("gb2312"); 
InBlock.gif
InBlock.gif            
byte[] bytes=gb.GetBytes(this.TextBox1.Text.Trim()) ;
InBlock.gif
InBlock.gif            
string lowCode = System.Convert.ToString(bytes[0],16); //取出元素1编码内容(两位16进制) 
InBlock.gif
            string hightCode = System.Convert.ToString(bytes[1],16);//取出元素2编码内容(两位16进制)
InBlock.gif
            this.Response.Write(lowCode + hightCode);
InBlock.gif
InBlock.gif
InBlock.gif            
//重新将编码组合成文字
InBlock.gif
            byte byte1=Convert.ToByte(lowCode,16); 
InBlock.gif            
byte byte2=Convert.ToByte(hightCode,16); 
InBlock.gif
InBlock.gif            
//将两个字节变量存储在字节数组中 
ExpandedSubBlockStart.gifContractedSubBlock.gif
            byte[] str_r=new byte[]dot.gif{byte1,byte2}
InBlock.gif
InBlock.gif            
//根据汉字编码的字节数组解码出中文汉字 
InBlock.gif
            string str1=gb.GetString((byte[])Convert.ChangeType(str_r, typeof(byte[]))); 
InBlock.gif
InBlock.gif            
this.Response.Write(str1);
ExpandedBlockEnd.gif        }

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

转载于:https://www.cnblogs.com/wolanda/archive/2005/07/21/197209.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值