2012/01/06

c# hex str transformation

Form looks like this...

Code is below...
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace StrHex
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void Form1_Load(object sender, EventArgs e)
  20.         {
  21.             EncodingInfo[] infos = Encoding.GetEncodings();
  22.             for (int i = 0; i < infos.Length; i++)
  23.                 comboBox1.Items.Add(string.Format("{0} - {1}", infos[i].DisplayName, infos[i].Name));
  24.  
  25.             if (comboBox1.Items.Count > 0)
  26.                 comboBox1.SelectedIndex = 0;
  27.         }
  28.  
  29.         private void button1_Click(object sender, EventArgs e)
  30.         {
  31.             string name = comboBox1.Text.Split(new string[] {" - "}, StringSplitOptions.None)[1];
  32.             Encoding encode = Encoding.GetEncoding(name);
  33.             if (encode != null)
  34.             {
  35.                 Byte[] bs = encode.GetBytes(textBox1.Text);
  36.                 if (bs.Length * 2 > textBox1.MaxLength)
  37.                     return;
  38.  
  39.                 StringBuilder sb = new StringBuilder();
  40.                 foreach (Byte b in bs)
  41.                     sb.AppendFormat("{0:X2}", b);
  42.                 textBox1.Text = sb.ToString();
  43.             }
  44.         }
  45.  
  46.         private void button2_Click(object sender, EventArgs e)
  47.         {
  48.             string name = comboBox1.Text.Split(new string[] {" - "}, StringSplitOptions.None)[1];
  49.             Encoding encode = Encoding.GetEncoding(name);
  50.             if (encode != null)
  51.             {
  52.                 if (textBox1.TextLength % 2 == 0)
  53.                 {
  54.                     ArrayList al = new ArrayList();
  55.                     for (int i = 0; i < textBox1.TextLength; i += 2)
  56.                     {
  57.                         Byte b = 0;
  58.                         string hex = textBox1.Text.Substring(i, 2);
  59.                         if (Byte.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out b))
  60.                             al.Add(b);
  61.                         else
  62.                             return;
  63.                     }
  64.                     textBox1.Text = encode.GetString((Byte[])al.ToArray(typeof(Byte)));
  65.                 }
  66.             }
  67.         }
  68.  
  69.         private void textBox1_KeyDown(object sender, KeyEventArgs e)
  70.         {
  71.             if (e.Control && e.KeyCode == Keys.A)
  72.                 textBox1.SelectAll();
  73.         }
  74.  
  75.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  76.         {
  77.             if (e.Control && e.KeyCode == Keys.A)
  78.             {
  79.                 textBox1.SelectAll();
  80.                 textBox1.Focus();
  81.             }
  82.  
  83.             if (e.Control && e.KeyCode == Keys.H)
  84.                 button1.PerformClick();
  85.  
  86.             if (e.Control && e.KeyCode == Keys.S)
  87.                 button2.PerformClick();
  88.  
  89.             if (e.Control && e.KeyCode == Keys.W)
  90.                 this.Close();
  91.         }
  92.  
  93.         private void textBox1_TextChanged(object sender, EventArgs e)
  94.         {
  95.             label2.Text = string.Format("Word Count: {0}", textBox1.TextLength);
  96.         }
  97.     }
  98. }
  99.  

沒有留言: