⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 快速地把各种源文件的Unicode码转换为汉字或相反
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;

namespace ConvertUnicode
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private string[] fnames;
		private System.Windows.Forms.Button button5;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(0, 224);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(656, 21);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(0, 296);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(664, 21);
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(56, 328);
			this.button1.Name = "button1";
			this.button1.TabIndex = 2;
			this.button1.Text = "单条转换";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(80, 24);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(368, 21);
			this.textBox3.TabIndex = 3;
			this.textBox3.Text = "GongGaoWindow.cs";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(488, 24);
			this.button2.Name = "button2";
			this.button2.TabIndex = 4;
			this.button2.Text = "转换";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 23);
			this.label1.TabIndex = 5;
			this.label1.Text = "文件名";
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 12;
			this.listBox1.Location = new System.Drawing.Point(80, 56);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(192, 136);
			this.listBox1.TabIndex = 6;
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.Multiselect = true;
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(312, 64);
			this.button3.Name = "button3";
			this.button3.TabIndex = 7;
			this.button3.Text = "添加文件";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(312, 128);
			this.button4.Name = "button4";
			this.button4.TabIndex = 8;
			this.button4.Text = "批量转换";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button5
			// 
			this.button5.Image = ((System.Drawing.Image)(resources.GetObject("button5.Image")));
			this.button5.Location = new System.Drawing.Point(480, 120);
			this.button5.Name = "button5";
			this.button5.TabIndex = 9;
			this.button5.Text = "button5";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(736, 358);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			string a=textBox1.Text;
			byte[] b=new byte[a.Length];
			char[] c=new char[a.Length];
			int i,j=0;
			StringBuilder g=new StringBuilder ();
			for(i=0;i<a.Length;i++)
			{
				if (a[i]=='\\')
				{
					i++;
					if (a[i]=='u') i++;
				}
				g.Append(a[i]);				
				j++;				
			}
			string h=g.ToString();
			string tmp;
			int yy1=0,yy2=0,aa=0,yy3=0,yy4=0,bb1=0,bb2=1;
			
			for(i=0;i<j;i++)
			{
				aa++;
				if(aa>4) 
				{
					b[bb1]=Convert.ToByte(yy4);
					b[bb2]=Convert.ToByte(yy2);
					aa=1;
					yy1=0;
					yy2=0;
					yy3=0;
					yy4=0;
					bb1=bb1+2;
					bb2=bb2+2;
				}
				tmp=h[i].ToString();
				switch (tmp)
				{
					case "a":
						tmp="10";
						break;
					case "b":
						tmp="11";
						break;
					case "c":
						tmp="12";
						break;
					case "d":
						tmp="13";
						break;
					case "e":
						tmp="14";
						break;
					case "f":
						tmp="15";
						break;
				}
				if(aa<=2)
				{
					yy1=int.Parse (tmp);
					yy2=yy2*16+yy1;
				}
				if(aa>2)
				{
					yy3=int.Parse (tmp);
					yy4=yy4*16+yy3;
				}
				
			}
			b[bb1]=Convert.ToByte(yy4);
			b[bb2]=Convert.ToByte(yy2);
			
			UnicodeEncoding d=new UnicodeEncoding();

			string ll=d.GetString(b);
			textBox2.Text =ll;
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			C(textBox3.Text);
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			openFileDialog1.Filter  ="cs文件|*.cs|所有文件|*.*";
			openFileDialog1.ShowDialog();
			fnames=openFileDialog1.FileNames;
			for(int i=0;i<fnames.Length;i++)
                listBox1.Items.Add(fnames[i]);
		}

		private void C(string filename)
		{
			FileStream s=new FileStream(filename,FileMode.Open,FileAccess.ReadWrite);
			StreamReader r=new StreamReader (s,Encoding.Default);
			FileStream ss=File.Create(Application.StartupPath +"\\tmp.txt");
			StreamWriter w=new StreamWriter (ss,Encoding.UTF8);
			//			Encoding er=r.CurrentEncoding;
			
			byte[] by=new byte[2];
			string a=r.ReadToEnd();
//			char[] fc=new char[s.Length ];
//			string slk=r.CurrentEncoding.EncodingName ;
//			r.ReadBlock (fc,0,(int)s.Length );
//			slk=r.CurrentEncoding.EncodingName ;
			Encoding ascii = Encoding.Default;
			char[]  fc=ascii.GetChars(ascii.GetBytes(a)); 
//			string a="";
			int b=a.Length;
			int i,last=0;
			bool flag=false,flag2=false;
			for(i=0;i<b;i++)
			{
				if(a[i]=='\"')
				{
					if(a[i-1]=='@') flag2=true;
					string ls=new string(fc,last,i-last+1);
					last=i+1;
					w.Write(ls);
					w.Flush();

					i++;
					while((a[i]!='\"')||((a[i]=='\"')&&(a[i-1]=='\\')&&(flag2==false)))
					{
						if((a[i]=='\"')&&(a[i-1]=='\\')&&(a[i-2]=='\\')) break;
						if((a[i]=='\\')&&(a[i+1]=='u')&&(flag2==false))
						{
							flag=true;
							ls=new string(fc,last,i-last);
							last=i;
							w.Write(ls);
							w.Flush ();
							i=i+2;
							last=last+2;

							int yy1=0,yy2=0,aa=0,yy3=0,yy4=0;
							string tmp;
							yy1=yy2=aa=yy3=yy4=0;
				
							for(int m=0;m<4;m++)
							{
								aa++;
								tmp=a[i+m].ToString();
								switch (tmp)
								{
									case "a":
										tmp="10";
										break;
									case "b":
										tmp="11";
										break;
									case "c":
										tmp="12";
										break;
									case "d":
										tmp="13";
										break;
									case "e":
										tmp="14";
										break;
									case "f":
										tmp="15";
										break;
								}
								if(aa<=2)
								{
									yy1=int.Parse (tmp);
									yy2=yy2*16+yy1;
								}
								if(aa>2)
								{
									yy3=int.Parse (tmp);
									yy4=yy4*16+yy3;
								}
					
							}//for m
							by[0]=Convert.ToByte(yy4);
							by[1]=Convert.ToByte(yy2);

							UnicodeEncoding d=new UnicodeEncoding();

							string ll=d.GetString(by);

							//							w.BaseStream.Seek(i-2,SeekOrigin.Begin);
							w.Write(ll);
							//							w.Write(ll);
							w.Flush();
							//							s.Seek(i+2,SeekOrigin.Begin);
						}//if((a[i]=='\\')&&(a[i+1]=='u'))
						
						if(flag)
						{
							i=i+4;
							last=last+4;
							flag=false;
						}
						else
						{
							i++;
						}
						if (i>=b) break;
					}//while
					if(flag2) flag2=false;
					
				}//if((a[i]=='\"') && (flag==false))
			}//for(i=0;i<b;i++)

			if(i>b) i=b;
			string ls1=new string(fc,last,i-last);
			//			last=i;
			w.Write(ls1);
			w.Flush ();

			//			s.Flush ();
			w.Close ();
			r.Close();			
			//			s.Close();
			
			File.Copy(Application.StartupPath +"\\tmp.txt",filename,true);
			File.Delete(Application.StartupPath +"\\tmp.txt");
			//			File.Delete("tmp"+textBox3.Text);
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			for(int i=0;i<fnames.Length;i++)
				C(fnames[i]);
		}

		private void button5_Click(object sender, System.EventArgs e)
		{
			string s="@ld@ll@";
			textBox1.Text =s;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -