字符ASCII转换

Posted on 2018-11-01 23:44  努力成长静待花开  阅读(386)  评论(0编辑  收藏  举报

实现效果:

关键知识:

实现代码:

 1     private void button1_Click(object sender, EventArgs e)
 2     {
 3         if (textBox1.Text != string.Empty) {//不为空
 4             if (Encoding.GetEncoding("unicode").//判断字是否为字符
 5                 GetBytes(new char[] { textBox1.Text[0] })[1] == 0)
 6             {       //转ASCII
 7                 textBox2.Text = Encoding.GetEncoding("unicode").
 8                     GetBytes(textBox1.Text)[0].ToString(); 
 9             }
10             else {
11                 textBox1.Text = string.Empty;
12                 MessageBox.Show("请输入正确字母");
13             }
14         }
15     }
16     private void button2_Click(object sender, EventArgs e)
17     {
18         if (textBox3.Text != string.Empty) { 
19             int temp;
20             if (int.TryParse(textBox3.Text, out temp))
21             {   //转换字符
22                 textBox4.Text = ((char)temp).ToString();
23             }
24             else {
25                 textBox1.Text = string.Empty;
26                 MessageBox.Show("请输入正确数字");
27             }
28         }
29     }