C# 自制 hex與str互轉小工具 成品與源碼分享 相互學習

阿里雲幸運卷,戳我領取

程序調試需要,做了一個 hex與str互轉小工具方便使用

比如,輸入hex 0x01與0x12,就在輸入框內寫 0112 (兩個字節)
會輸出這兩個字節的ASCII:30313132 (四個字節)

需要的小夥伴自行點擊下載。

點擊下載小工具
在這裏插入圖片描述

初始設計操作界面如圖:
在這裏插入圖片描述

代碼如下:

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace HEX與字符串轉換工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = null;
            int hex = 0;
            if (textBox1.Text.Length % 2 == 1)
            {
                textBox2.Text = "輸入錯誤!請輸入2的倍數個字符!";
                return;
            }
            for(int i = 0;i < textBox1.Text.Length;i++)
            {
                if((textBox1.Text[i]>='0'&& textBox1.Text[i]<='9')||
                    (textBox1.Text[i] >= 'a' && textBox1.Text[i] <= 'f') ||
                    (textBox1.Text[i] >= 'A' && textBox1.Text[i] <= 'F'))
                {
                    hex = (int)textBox1.Text[i];
                    str += hex.ToString("X2");
                }
                else
                {
                    str += "XX";
                }

            }
            textBox2.Text = str;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string hexABC = "0123456789ABCDEF";
            string hexabc = "0123456789abcdef";
            string str = null;
            byte[] buff = new byte[textBox2.Text.Length];
            int i;
            byte j;
            if (textBox2.Text.Length % 4 != 0)
            {
                textBox1.Text = "輸入錯誤!請輸入4的倍數個字符!";
                return;
            }
            for (i = 0; i < textBox2.Text.Length; i++)
            {
                for (j = 0; j < 16; j++)
                {
                    if (textBox2.Text[i] == hexABC[j])
                    {
                        buff[i] = j;
                        break;
                    }
                    else if (textBox2.Text[i] == hexabc[j])
                    {
                        buff[i] = j;
                        break;
                    }
                    if (j == 15)
                    {
                        textBox1.Text = "輸入錯誤!僅允許輸入0-f!";
                        return;
                    }
                }
            }
            int point = 0;
            byte[] result = new byte[textBox2.Text.Length / 2];
            for (i = 0; i < textBox2.Text.Length / 2; i++)
            {
                result[i] = (byte)(buff[point] * 16 + buff[point + 1]);
                point += 2;
                if ((result[i] >= '0' && result[i] <= '9') ||
                    (result[i] >= 'a' && result[i] <= 'f') ||
                     (result[i] >= 'A' && result[i] <= 'F'))
                {
                    str += ((char)result[i]).ToString();
                }
                else
                {
                    textBox1.Text = "輸入錯誤!數值錯誤!";
                    return;
                }
               

            }
            textBox1.Text = str;

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("https://blog.csdn.net/tiantangmoke/article/category/8920863");
        }


    }
}






發佈了39 篇原創文章 · 獲贊 41 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章