×

Výstrahy

JUser: :_load: Nemôžem načítať používateľa s ID: 75
streda, 22 február 2012 09:50 Written by 2454 times
Rate this item
(4 votes)

C# - ASCII convertor

ASCIIto je užitocný nástroj pre každého, kto pracuje s ASCII znakmi a potrebuje ich previest na hexadecimálne alebo dekadické čísla a spätne HEX a DEC na ASCI.

ASCI convertor

C# kód :

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace ASCIIto
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void ASCIItoHEX(string ASCIData ,out string HEXdata)
        {
            HEXdata = String.Empty;
            byte[] temp;
            try
            {
                temp = ASCIIEncoding.ASCII.GetBytes(ASCIData);
                foreach (byte b1 in temp)
                {
                    HEXdata += b1.ToString("X") + " ";
                }
            }
            catch
            {
            }
        }
        private void ASCIItoDEC(string ASCIIData, out string DECdata)
        {
            DECdata = String.Empty;
            byte[] temp;
            try
            {
                temp = ASCIIEncoding.ASCII.GetBytes(ASCIIData);
                foreach (byte b1 in temp)
                {
                    DECdata += b1.ToString() + " ";
                }
                DECdata.TrimEnd(' ');
            }
            catch
            {
            }
        }
        private void DECtoASCII(string DECdata,out string ASCIIData )
        {
            ASCIIData = String.Empty;
            string[] temp;           
            try
            {
                temp = DECdata.Split(new string[] { " " }, StringSplitOptions.None);
                foreach (string b1 in temp)
                {
                    ASCIIData += ((char)Convert.ToInt16(b1)).ToString();                    
                }    
            }
            catch
            {
            }
        }
        private void HEXtoASCII(string HEXdata, out string ASCIIData)
        {
            ASCIIData = String.Empty;
            string[] temp;
            try
            {
                temp = HEXdata.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string b1 in temp)
                {
                    ASCIIData += ((char)Convert.ToInt16(b1,16)).ToString();
                }
            }
            catch
            {
            }
        }
        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            string HEXdata = String.Empty;
            string DECdata = this.textBox2.Text;
            string ASCIIdata = String.Empty;
            this.DECtoASCII(DECdata, out ASCIIdata);
            this.textBox1.Text = ASCIIdata;
            this.ASCIItoHEX(ASCIIdata, out HEXdata);
            this.textBox3.Text = HEXdata;  
        }
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            string HEXdata = String.Empty;
            string DECdata = String.Empty;
            string ASCIIdata = this.textBox1.Text;
            this.ASCIItoHEX(ASCIIdata, out HEXdata);
            this.ASCIItoDEC(ASCIIdata, out DECdata);
            this.textBox3.Text = HEXdata;
            this.textBox2.Text = DECdata;
        }
        private void textBox3_KeyUp(object sender, KeyEventArgs e)
        {
            string HEXdata = this.textBox3.Text;
            string DECdata = String.Empty;
            string ASCIIdata = String.Empty;
            this.HEXtoASCII(HEXdata, out ASCIIdata);
            this.textBox1.Text = ASCIIdata;
            this.ASCIItoDEC(ASCIIdata, out DECdata);
            this.textBox2.Text = DECdata;  
        }
        private void label4_Click(object sender, EventArgs e)
        {
            Process.Start(@"http://www.projectik.eu");
        }
        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' ||
                e.KeyChar >= 'a' && e.KeyChar <= 'f' ||
                e.KeyChar >= 'A' && e.KeyChar <= 'F' ||
                e.KeyChar == ' ' ||
                e.KeyChar == '\b')
                e.Handled = false;
            else
                e.Handled = true;
        }
        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9'||            
                e.KeyChar == ' ' ||
                e.KeyChar == '\b')
                e.Handled = false;
            else
                e.Handled = true;
        }
      
      
    }
}
Last modified on pondelok, 19 december 2016 21:27
Ing.Jaroslav Vadel

Som zakladateľom www.projectik.eu.

Hrám sa na programátora, ktorý ovláda:

c#,cpp,java,unity3d,php,html,NI testand,NI Vision Builder,Cognex In-Sight,NI LabView

"Naprogramovať program, ktorý funguje vie každy. Ale to, že program funguje ešte neznamena, že je napísany správne "

Website: www.projectik.eu