ASP.NET中的编码
发布日期:2022-02-05 18:27:40 浏览次数:29 分类:技术文章

本文共 8242 字,大约阅读时间需要 27 分钟。

using System;

using System.Text;
using System.IO;
namespace CharAndString
{
 class Class1
 {
  static void Main(string[] args)
  {
   //字符串类 String 中的内容是 UNICODE 字符串:
   string str = "中文123";
   // 得到长度为 5,因为是 5 个字符
   Console.WriteLine(str.Length);
   //字符串和字节串相互转化
   // 按照 GB2312 得到字节(得到多字节字符串)
   Encoding gb=Encoding.GetEncoding("gb2312");
   byte[] bytes = gb.GetBytes(str);
   char[] chars = gb.GetChars(bytes);//从字节按照 GB2312 得到字符数组  
   str = gb.GetString(bytes);// 从字节按照 GB2312 得到 UNICODE 字符串
   // 要将 String 按照某种编码写入文本文件,有两种方法:
   // 第一种办法:用 Stream 类写入已经按照指定编码转化好的字节串
   StreamWriter sw = new StreamWriter("1.txt");
   sw.Write(chars);
   sw.Close();
   // 第二种办法:构造指定编码的 Writer 来写入字符串
   StreamWriter sw2 = new StreamWriter("2.txt",false,gb);
   sw2.Write(str);
   sw2.Close();
   /* 最后得到的 1.txt 和 2.txt 都是 7 个字节 */  }
 }
}

using System;

using System.Text;
namespace WrongCode
{
 class Class1
 {
  static void Main(string[] args)
  {
   //字符串类 String 中的内容是 UNICODE 字符串:
   string str = "中国人123";
   // 按照 GB2312 得到字节(得到多字节字符串)
   Encoding Client=Encoding.GetEncoding("gb2312");
   byte[] bytes = Client.GetBytes(str);
    //服务端按照gb2312正确解码
   Encoding ServerOK = Encoding.GetEncoding("gb2312");
   string strServer = ServerOK.GetString(bytes);
   Console.WriteLine("正确转换后的字符串为:"+strServer);
   //服务端按照Unicode错误解码
   strServer = Encoding.Unicode.GetString(bytes);
   Console.WriteLine("错误转换后的字符串为:"+strServer);
  }
 }
}

using System;

using System.Text;
namespace WrongCode
{
 class Class1
 {
    static void Main(string[] args)
  {
   //字符串类 String 中的内容是 UNICODE 字符串:
   string str = "中国人123";
   // 按照 GB2312 得到字节(得到多字节字符串)
   Encoding Client=Encoding.GetEncoding("gb2312");
   byte[] bytes = Client.GetBytes(str);   
   //服务端按照gb2312正确解码
   Encoding ServerOK = Encoding.GetEncoding("gb2312");
   string strServer = ServerOK.GetString(bytes);
   Console.WriteLine("正确转换后的字符串为:"+strServer);
   //服务端按照Unicode错误解码
   strServer = Encoding.Unicode.GetString(bytes);
   Console.WriteLine("错误转换后的字符串为:"+strServer);
  }
 }
}

  private void Page_Load(object sender, System.EventArgs e)
  {
   Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
   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);
    GB2312Function("");
   }
  }
  //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);//对要在浏览器中显示的字符串进行编码。
  }
  void GB2312Function(string str)
  {
   string utfinfo = "document.write(/"alert('aa你好么??');/");";
   string gb2312info = "";
   Encoding utf8 = Encoding.UTF8;
   Encoding gb2312 = Encoding.GetEncoding("gb2312");    
   byte[] unicodeBytes = utf8.GetBytes(utfinfo);
   byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes); // Convert the new byte[] into a char[] and then into a string.
   char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
   gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
   gb2312info = new string(asciiChars);
   Label_5.Text=gb2312info;
  }

生产文字验证码

  private void Page_Load(object sender, System.EventArgs e)
  {
   //获取GB2312编码页(表)
   Encoding gb=Encoding.GetEncoding("gb2312");
 //调用函数产生4个随机中文汉字编码
   object[] bytes=CreateRegionCode(4); 
   //根据汉字编码的字节数组解码出中文汉字
   string str1=gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
   string str2=gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
   string str3=gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
   string str4=gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
   string strKey = str1+str2+str3+str4;
   Session["regcode"] = strKey;
   byte[] data = gb.GetBytes(strKey);
   Response.ContentEncoding = gb;
   Response.OutputStream.Write(data,0,data.Length);
  }
  /*
  此函数在汉字编码范围内随机创建含两个元素的十六进制字节数组,每个字节数组代表一个汉字,并将
  四个字节数组存储在object数组中。
  参数:strlength,代表需要产生的汉字个数
  */
  public static object[] CreateRegionCode(int strlength)
  {
   //定义一个字符串数组储存汉字编码的组成元素
   string[] rBase=new String [16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
   Random rnd=new Random();
   //定义一个object数组用来
   object[] bytes=new object[strlength];
   /**//*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
每个汉字有四个区位码组成
区位码第1位和区位码第2位作为字节数组第一个元素
区位码第3位和区位码第4位作为字节数组第二个元素
*/
   for(int i=0;i<strlength;i++)
   {
    //区位码第1位
    int r1=rnd.Next(11,14);
    string str_r1=rBase[r1].Trim();
    //区位码第2位
    rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);//更换随机数发生器的种子避免产生重复值
     int r2;
    if (r1==13)
    {
     r2=rnd.Next(0,7);
    }
    else
    {
     r2=rnd.Next(0,16);
    }
    string str_r2=rBase[r2].Trim();
    //区位码第3位
    rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i);
    int r3=rnd.Next(10,16);
    string str_r3=rBase[r3].Trim();
    //区位码第4位
    rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i);
    int r4;
    if (r3==10)
    {
     r4=rnd.Next(1,16);
    }
    else if (r3==15)
    {
     r4=rnd.Next(0,15);
    }
    else
    {
     r4=rnd.Next(0,16);
    }
    string str_r4=rBase[r4].Trim();
    //定义两个字节变量存储产生的随机汉字区位码
    byte byte1=Convert.ToByte(str_r1 + str_r2,16);
    byte byte2=Convert.ToByte(str_r3 + str_r4,16);
    //将两个字节变量存储在字节数组中
    byte[] str_r=new byte[]{byte1,byte2};
    //将产生的一个汉字的字节数组放入object数组中
    bytes.SetValue(str_r,i);
   }
   return bytes;
  }
}

图像保存到XML中

  private void btnUpload_Click(object sender, System.EventArgs e)
  {
   //得到用户要上传的文件名
   string strFilePathName = loFile.PostedFile.FileName;
   string strFileName = Path.GetFileName(strFilePathName);
   int FileLength = loFile.PostedFile.ContentLength;
   if(FileLength<=0)
    return;
   try
   {
    Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
    Stream StreamObject = loFile.PostedFile.InputStream; //建立数据流对像
    //读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
    StreamObject.Read(FileByteArray,0,FileLength);
    string fileName = Server.MapPath(".//WriteXml.xml");   //要打开的文件   
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(fileName);
    XmlNode root=xmlDoc.SelectSingleNode("dbImage");//查找<dbGuest>
    XmlNodeList xnl=xmlDoc.SelectSingleNode("dbImage").ChildNodes;
    int nIndex = xnl.Count;
    //以下添加新结点
    XmlElement xe1=xmlDoc.CreateElement("Image");//创建一个<User>节点
    XmlElement xesub1=xmlDoc.CreateElement("ImageID");
    xesub1.InnerText=nIndex.ToString();//设置文本节点
    xe1.AppendChild(xesub1);//添加到<User>节点中
    XmlElement xesub2=xmlDoc.CreateElement("ImageContentType");
    xesub2.InnerText=loFile.PostedFile.ContentType;
    xe1.AppendChild(xesub2);
    XmlElement xesub3=xmlDoc.CreateElement("ImageSize");
    xesub3.InnerText=FileLength.ToString();
    xe1.AppendChild(xesub3);
    XmlElement xesub4=xmlDoc.CreateElement("ImageDescription");
    xesub4.InnerText=tbDescription.Text;
    xe1.AppendChild(xesub4);
    XmlElement xesub5=xmlDoc.CreateElement("ImageData");    
    xesub5.InnerText= Convert.ToBase64String(FileByteArray);
    xe1.AppendChild(xesub5);
    root.AppendChild(xe1);//添加到<dbGuest>节点中
    xmlDoc.Save(fileName);
    Response.Redirect("ShowAllImg.aspx");
   }
   catch
   {
   }
  }
  private void Page_Load(object sender, System.EventArgs e)
  {
   string fileName = Server.MapPath("./WriteXml.xml");   //要打开的文件
   DataSet ds = new DataSet();
   ds.ReadXml(fileName);
   dgShow.DataSource = ds.Tables["image"].DefaultView;
   dgShow.DataBind();
  }
private void Page_Load(object sender, System.EventArgs e)
  {
   int ImgID = Convert.ToInt32(Request.QueryString["ID"]); //ID为图片ID
   //建立数据库链接
   string fileName = Server.MapPath(".//WriteXml.xml");   //要打开的文件   
   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.Load(fileName);
   XmlNodeList node =  xmlDoc.SelectSingleNode("//Image[ImageID='"+ImgID.ToString()+"']").ChildNodes;
   if(node!=null)
   {
    string strType = node.Item(1).InnerText;
    string strData =node.Item(4).InnerText;
    int nSize = int.Parse(node.Item(2).InnerText);
    Response.ContentType = strType;//设定输出文件类型
    //输出图象文件二进制数制
    Response.OutputStream.Write(Convert.FromBase64String(strData), 0, nSize);
    Response.End();
    //也可以保存为图像
    //   FileStream fs = new FileStream(@"C:/aa.BMP", FileMode.OpenOrCreate, FileAccess.Write);
    //   fs.Write((Convert.FromBase64String(strData), 0,nSize);
    //   fs.Close();
 }

转载地址:https://blog.csdn.net/lisong58420/article/details/2302828 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:领悟 JavaScript 中的面向对象
下一篇:js页面对象控制

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年03月04日 23时56分54秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

java3d立方体_3d立方体贴图 2019-04-21
java ajax教程_(转)JAVA AJAX教程第三章—AJAX详细讲解 2019-04-21
java operators_A guide to Java Operators 2019-04-21
java socket调试_JAVA实现SOCKET多客户端通信的案例 2019-04-21
java 使用或覆盖了已过时的api_JAVA使用或覆盖了已过时的 API 2019-04-21
java 图片旋转保存_Java 对图片90度旋转 2019-04-21
用java实现文学研究助手_数据结构文学研究助手 C语言代码实现(带源码+解析)... 2019-04-21
java gc的几种方式_GC 的三种基本实现方式 2019-04-21
wget linux java 32_通过wget在Linux上下载Java JDK会显示在许可证页面上 2019-04-21
babylonjs 设置面板位置_babylonjs 空间坐标转为屏幕坐标 2019-04-21
oracle里面如何查询sqlid,CSS_oracle中如何查看sql, --查询表状态:  select uo.O - phpStudy... 2019-04-21
oracle 查询中用case,oracle case when 在查询时候的用法。 2019-04-21
oracle正在运行的程序包,ORACLE PL/SQL编程详解之程序包的创建与应用 2019-04-21
php局部页面滚动,在访问另一页面后保留浏览器滚动位置 - php 2019-04-21
jmeter运行linux命令行,Jmeter在linux上运行(命令行运行Jmeter) 2019-04-21
linux服务器怎么添加站点,如何增加站点或虚拟主机及文件说明 2019-04-21
linux系统输入指令,Linux系统基础 - 基本操作命令 2019-04-21
linux设备管理命令,Linux命令(设备管理).doc 2019-04-21
linux 中文utf-8转gbk编码,Linux平台下 GBK编码转UTF-8编码 2019-04-21
linux安装软件在boot,在Linux系统上安装Spring boot应用的教程详解 2019-04-21