using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace StrHex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
EncodingInfo[] infos = Encoding.GetEncodings();
for (int i = 0; i < infos.Length; i++)
comboBox1.Items.Add(string.Format("{0} - {1}", infos[i].DisplayName, infos[i].Name));
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
string name
= comboBox1
.Text.Split(new string[] {" - "}, StringSplitOptions
.None)[1];
Encoding encode = Encoding.GetEncoding(name);
if (encode != null)
{
Byte[] bs = encode.GetBytes(textBox1.Text);
if (bs.Length * 2 > textBox1.MaxLength)
return;
StringBuilder sb
= new StringBuilder
();
foreach (Byte b in bs)
sb.AppendFormat("{0:X2}", b);
textBox1.Text = sb.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
string name
= comboBox1
.Text.Split(new string[] {" - "}, StringSplitOptions
.None)[1];
Encoding encode = Encoding.GetEncoding(name);
if (encode != null)
{
if (textBox1.TextLength % 2 == 0)
{
ArrayList al
= new ArrayList
();
for (int i = 0; i < textBox1.TextLength; i += 2)
{
Byte b = 0;
string hex = textBox1.Text.Substring(i, 2);
if (Byte.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out b))
al.Add(b);
else
return;
}
textBox1
.Text = encode
.GetString((Byte[])al
.ToArray(typeof(Byte)));
}
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
textBox1.SelectAll();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
textBox1.SelectAll();
textBox1.Focus();
}
if (e.Control && e.KeyCode == Keys.H)
button1.PerformClick();
if (e.Control && e.KeyCode == Keys.S)
button2.PerformClick();
if (e.Control && e.KeyCode == Keys.W)
this.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
label2.Text = string.Format("Word Count: {0}", textBox1.TextLength);
}
}
}