Laden...

Encoding Tester: Zeigt den (256er-)Zeichensatz eines Encodings oder eine Datei im gewählten Encoding

Erstellt von Programmierhans vor 18 Jahren Letzter Beitrag vor 11 Jahren 19.591 Views
Programmierhans Themenstarter:in
4.221 Beiträge seit 2005
vor 18 Jahren
Encoding Tester: Zeigt den (256er-)Zeichensatz eines Encodings oder eine Datei im gewählten Encoding

Hallo Leute

Importieren von Daten ... exportieren von Daten welche von anderen Programmen geschrieben wurden.... Wer kennt sie nicht die leidige Code-Page Problematik...

Daher habe ich mal schnell einen Code-Page-Tester geschrieben. Dieser kann:

  • Ein File einlesen und in einer beliebigen Code-Page anzeigen (somit findet man schneller die richtige Code-Page für ein existierendes File....

  • Den Zeichensatz von 1-255 anzeigen um sich schnell einen Ueberblick über den Inhalt einer CodePage zu verschaffen....

Hier der Code:




using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;

namespace ZeichensatzTester
{
	/// <summary>
	/// Nach einer Idee von Programmierhans
	/// </summary>
	public class ZeichenSatzForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cboEncoding;
		private System.Windows.Forms.CheckBox chkAlleEncodings;
		private System.Windows.Forms.Button btnFile;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.TextBox txtFile;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ZeichenSatzForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			this.FillEncodingCombo(437,1258);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.cboEncoding = new System.Windows.Forms.ComboBox();
			this.chkAlleEncodings = new System.Windows.Forms.CheckBox();
			this.btnFile = new System.Windows.Forms.Button();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.txtFile = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox1.Location = new System.Drawing.Point(0, 120);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(808, 336);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 88);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 23);
			this.label1.TabIndex = 1;
			this.label1.Text = "Encoding:";
			// 
			// cboEncoding
			// 
			this.cboEncoding.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.cboEncoding.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboEncoding.Location = new System.Drawing.Point(80, 88);
			this.cboEncoding.Name = "cboEncoding";
			this.cboEncoding.Size = new System.Drawing.Size(720, 21);
			this.cboEncoding.TabIndex = 2;
			this.cboEncoding.SelectionChangeCommitted += new System.EventHandler(this.cboEncoding_SelectionChangeCommitted);
			// 
			// chkAlleEncodings
			// 
			this.chkAlleEncodings.Location = new System.Drawing.Point(8, 8);
			this.chkAlleEncodings.Name = "chkAlleEncodings";
			this.chkAlleEncodings.Size = new System.Drawing.Size(728, 24);
			this.chkAlleEncodings.TabIndex = 3;
			this.chkAlleEncodings.Text = "Alle Encodings (dauert extrem lange !! da alle Codepages von 0-65535 durchgegange" +
				"n werden und die meisten davon gibts gar nicht)";
			this.chkAlleEncodings.CheckedChanged += new System.EventHandler(this.chkAlleEncodings_CheckedChanged);
			// 
			// btnFile
			// 
			this.btnFile.Location = new System.Drawing.Point(8, 40);
			this.btnFile.Name = "btnFile";
			this.btnFile.Size = new System.Drawing.Size(64, 23);
			this.btnFile.TabIndex = 4;
			this.btnFile.Text = "File";
			this.btnFile.Click += new System.EventHandler(this.btnFile_Click);
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.Filter = "TextFiles (*.txt)|*.txt|All Files (*.*)|*.*";
			// 
			// txtFile
			// 
			this.txtFile.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtFile.Location = new System.Drawing.Point(80, 40);
			this.txtFile.Name = "txtFile";
			this.txtFile.ReadOnly = true;
			this.txtFile.Size = new System.Drawing.Size(712, 20);
			this.txtFile.TabIndex = 5;
			this.txtFile.Text = "Wenn Sie ein File auswählen wird dieses geladen.... anderenfalls werden alle Zeic" +
				"hen von 1-256 dargestellt.";
			// 
			// ZeichenSatzForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(800, 453);
			this.Controls.Add(this.txtFile);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.btnFile);
			this.Controls.Add(this.chkAlleEncodings);
			this.Controls.Add(this.cboEncoding);
			this.Controls.Add(this.label1);
			this.Name = "ZeichenSatzForm";
			this.Text = "Encoding Tester by Programmierhans";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new ZeichenSatzForm());
		}

		private void FillEncodingCombo(int pStartAtCodePage, int pEndAtCodePage)
		{
			this.cboEncoding.Items.Clear();
			for (int i=pStartAtCodePage;i<=pEndAtCodePage;i++)
			{
				try
				{
					Encoding enc=Encoding.GetEncoding(i);
					if (enc!=null)
					{
						EncodingHolder ench=new EncodingHolder(enc);
						this.cboEncoding.Items.Add(ench);
					}
				}
				catch{}
			}
		}

		private void FillText()
		{
			if (this.cboEncoding.SelectedIndex>-1)
			{
				this.textBox1.Text=string.Empty;
				EncodingHolder ench=(EncodingHolder)this.cboEncoding.SelectedItem;
				Encoding enc=ench.Encoding;
				if (this.txtFile.Text!=null && System.IO.File.Exists(this.txtFile.Text))
				{
					//File einlesen und anzeigen
					using (System.IO.StreamReader sr=new System.IO.StreamReader(this.txtFile.Text,enc))
					{
						this.textBox1.Text=sr.ReadToEnd();
					}
				}
				else
				{
					//alle anzeigen
					StringBuilder sb=new StringBuilder();
					for (int i=1;i<256;i++) //0 ist string-Terminator 
					{
						string zeichen=enc.GetString(new byte[]{(byte)i},0,1);;
						sb.AppendFormat("({0}) = {1}\t",i.ToString().PadLeft(3,'0'),zeichen);
					}
					this.textBox1.Text=sb.ToString();
				}
			}

		}
		private void cboEncoding_SelectionChangeCommitted(object sender, System.EventArgs e)
		{
			this.FillText();
		}

		private void chkAlleEncodings_CheckedChanged(object sender, System.EventArgs e)
		{
			Cursor.Current=Cursors.WaitCursor;
			this.chkAlleEncodings.Enabled=false;
			int intStart=this.chkAlleEncodings.Checked ? 0:437;
			int intEnd=this.chkAlleEncodings.Checked ? 65535:1258;
			this.FillEncodingCombo(intStart,intEnd);
			this.chkAlleEncodings.Enabled=true;
			Cursor.Current=Cursors.Default;
		}

		private void btnFile_Click(object sender, System.EventArgs e)
		{
			if (this.openFileDialog1.ShowDialog()==DialogResult.OK)
			{
				this.txtFile.Text=this.openFileDialog1.FileName;
			}
			else
			{
				this.txtFile.Text=string.Empty;
			}
			this.FillText();

		}
	}
	class EncodingHolder
	{
		private Encoding _Encoding=null;
		public EncodingHolder(Encoding pEncoding)
		{
			this._Encoding=pEncoding;
		}
		public Encoding Encoding
		{
			get{return this._Encoding;}
		}
		public override string ToString()
		{
			return string.Format("CodePage: {0} EncodingName: {1}",this._Encoding.CodePage,this._Encoding.EncodingName);
		}

	}
}



Für die faulen unter euch hab ich das Kompilat auch noch attached..

Früher war ich unentschlossen, heute bin ich mir da nicht mehr so sicher...

49.485 Beiträge seit 2005
vor 11 Jahren

hier noch ein Screenshot (wenn man eine Datei ausgewählt hätte, würde diese in dem gewählten Encoding angezeigt)