[我的ASP.net学习历程]编码转换http://www.21tx.com/dev/2004/09/21/12011.html

<%@ Page Language=" C#" AutoEventWireup="True" Debug="true" ValidateRequest="false" %>
<Script language="C#" runat="server">
void Page_Load(Object Sender,EventArgs e){
 if (!Page.IsPostBack){
  Response.Write("还没有输入任何字符!<br><br>");
  }
 else{
  Label_1.Text="";
  Label_2.Text="";
  Label_0.Text=Convert.ToString((TextBox_1.Text).Length);
 //ASCII编码输出
  ByteFunction(TextBox_1.Text);
 //字符码输出
  CharFunction(TextBox_1.Text);
 //HTML编码输出
  HtmlFunction(TextBox_1.Text);
 }
}
//ASCII编码输出函数
void ByteFunction(string str){
 Byte[] MyBytes=System.Text.Encoding.ASCII.GetBytes(str);
 for (int i=0;i<str.Length;i++){
  Label_2.Text+=Convert.ToString(MyBytes[i])+" ";
 }
}
//字符码输出函数
void CharFunction(string str){
 Byte[] MyBytes=System.Text.Encoding.ASCII.GetBytes(str);
 Char[] MyChars=System.Text.Encoding.ASCII.GetChars(MyBytes);
 //Label_2.Text=new string(MyChars);
 for (int i=0;i<str.Length;i++){
  Label_1.Text+=Convert.ToString(MyChars[i])+" ";
 }
}
//HTML编码输出函数
void HtmlFunction(string str){
 string Str_Html=(Server.HtmlEncode(str));
 Label_3.Text=Str_Html;
 Label_4.Text=Server.HtmlEncode(Str_Html);
 }
</Script>
<form runat="server">
请输入字符串:
 < ASP:TextBox ID="TextBox_1" runat="server"/>
 <asp:button ID="Button_1" Text="提交" runat="server"/>
</form>
您输入的字符串长度:
<asp:label ID="Label_0" runat="server"/><br>
字符码为:
<asp:label ID="Label_1" runat="server"></asp:label><br>
ASCII码编码:
<asp:label ID="Label_2" runat="server"></asp:label><br>
HTML编码为:
<asp:label ID="Label_3" runat="server"></asp:label><br>
HTML编码以字符输出:
<asp:label ID="Label_4" runat="server"/><br> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值