ASCII码转换

 

ASCII码转换

1. int.TryParse(string,out intNum)

2. 判断是否字母的原理

Encoding.GetEncoding("unicode").GetBytes(new char[] { textBoxAlpha.Text[0] })[1] == 0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GetAsciiNum
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnToNum_Click(object sender, EventArgs e)
        {
            if(textBoxAlpha.Text==string.Empty)
            {
                MessageBox.Show("请输入字母!", "提示");
                return;
            }
            //判断是否是字母
            if (Encoding.GetEncoding("unicode").GetBytes(new char[] { textBoxAlpha.Text[0] })[1] == 0)
            {
                //得到字符的ASCII码值
                textBoxResultNum.Text = Encoding.GetEncoding("unicode").GetBytes(textBoxAlpha.Text)[0].ToString();

            }

        }

        private void btnToal_Click(object sender, EventArgs e)
        {
            if (textBoxNum.Text == string.Empty)
            {
                MessageBox.Show("请输入数字!", "提示");
                return;
            }
            int num;
            if(int.TryParse(textBoxNum.Text,out num))//尝试解析成数字
            {
                textBoxResultAlpha.Text = ((char)num).ToString();
            }
            else
            {
                MessageBox.Show("请输入数字!", "提示");
                return;
            }
        }
    }
}

效果图:

 

posted @ 2018-12-14 11:36  随时静听  阅读(1080)  评论(0编辑  收藏  举报