以程序【字母与ASCII码转换器】为例,如何实现字母与ASCII码转换?
方法如下:
字母转换成ASCII码方法
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if(Encoding.GetEncoding("unicode").GetBytes(new char[] { textBox1.Text[0] })[1] == 0)
{
textBox2.Text = Encoding.GetEncoding("unicode").GetBytes(textBox1.Text)[0].ToString();
}
else
{
textBox2.Text = "请输入字母!";
}
}
}
ASCII码转换成字母方法
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text != "")
{
int a;
if(int.TryParse(textBox3.Text,out a))
{
textBox4.Text = ((char)a).ToString();
}
else
{
textBox4.Text = "请输入正确的ASCII码";
}
}
}
本文由查霆原创,转载需授权。原文地址:http://www.zhating.cn/index.php/post/29.html