目录
一、关于ASCⅡ及与字符互转
1.主要用到Encoding对象的GetBytes方法
2.Char显式转换为数值类型得到ASCⅡ
二、实例
三、生成效果
四、程序中的一些知识点
1.IsLetterOrDigit()
2.GetBytes()
3.TryParse(string, out int)
一、关于ASCⅡ及与字符互转
ASCⅡ(American Standard Code for Information Interchange,美国信息互换标准代码)是基于拉丁字母的编码系统,也是现今最通用的单字节编码系统。在程序设计中,可以方便地将字母转换为ASCⅡ码,也可以将ASCII码方便地转换为字母。
1.主要用到Encoding对象的GetBytes方法
Encoding对象的GetBytes方法接收一个字符串或字符数组作为参数,最后返回字节数组,可以根据字节数组得到字母的ASCⅡ码。
string P_str_temp ="abc";
Encoding P_encoding =Encoding.GetEncoding("unicode");
byte[]P_byte =P_encoding.GetBytes(P_str_temp);
string P_str=P_byte[0].ToString();
使用Encoding类的GetEncoding静态方法得到Encoding对象,然后调用Encoding对象的GetBytes方法,该方法接收一个字符串或字符数组作为参数,最后GetBytes方法会返回字节数组对象,可以根据字节数组的第0个索引来得到字符串中第一个字母的ASCⅡ码。
2.Char显式转换为数值类型得到ASCⅡ
字符Char是值类型,它总是表示成16位Unicode代码值。
现在已经了解到Char是值类型,如果将Char显式转换为数值类型,可以方便地得到ASCⅡ码值。相反,如果将ASCⅡ码数值强制转换为Char,将会得到一个Char对象。
二、实例
// 字符与ASCII相互转换
using System.Text;namespace _036
{public partial class Form1 : Form{private GroupBox? groupBox1;private Button? button2;private Button? button1;private TextBox? textBox1;private TextBox? textBox2;private TextBox? textBox3;private TextBox? textBox4;public Form1(){InitializeComponent();Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // button2// button2 = new Button{Location = new Point(117, 58),Name = "button2",Size = new Size(91, 23),TabIndex = 6,Text = "ASCII转字符",UseVisualStyleBackColor = true};button2.Click += Button2_Click;// // button1// button1 = new Button{Location = new Point(117, 29),Name = "button1",Size = new Size(91, 23),TabIndex = 5,Text = "字符转ASCII",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // textBox1// textBox1 = new TextBox{Location = new Point(6, 29),Name = "textBox1",Size = new Size(100, 23),TabIndex = 1};// // textBox2// textBox2 = new TextBox{Location = new Point(219, 29),Name = "textBox2",Size = new Size(100, 23),TabIndex = 2};// // textBox3// textBox3 = new TextBox{Location = new Point(6, 58),Name = "textBox3",Size = new Size(100, 23),TabIndex = 3};// // textBox4// textBox4 = new TextBox{Location = new Point(219, 58),Name = "textBox4",Size = new Size(100, 23),TabIndex = 4};// // groupBox1// groupBox1 = new GroupBox{Location = new Point(12, 14),Name = "groupBox1",Size = new Size(325, 100),TabIndex = 0,TabStop = false,Text = "字符与ASCII相互转换"};groupBox1.Controls.Add(button2);groupBox1.Controls.Add(button1);groupBox1.Controls.Add(textBox1);groupBox1.Controls.Add(textBox2);groupBox1.Controls.Add(textBox3);groupBox1.Controls.Add(textBox4);groupBox1.SuspendLayout();// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(349, 126);Controls.Add(groupBox1);Name = "Form1";StartPosition = FormStartPosition.CenterScreen;Text = "字符与ASCII互转"; groupBox1.ResumeLayout(false);groupBox1.PerformLayout(); }/// <summary>/// 字母转ASCII/// 注释掉的部分异常:/// Index was outside of the bounds of the array/// 未处理的异常:System.IndexOutOfRangeException:索引超出数组的范围(在第一个if语句处)/// 修改后,正常了/// </summary>private void Button1_Click(object? sender, EventArgs e){if (textBox1!.Text != string.Empty) //判断输入是否为空{/*if (Encoding.GetEncoding("unicode"). //判断输入是否为字符GetBytes(new char[] { textBox2!.Text[0] })[1] == 0) */ if (char.IsLetterOrDigit(textBox1.Text.ToCharArray()[0])) //判断输入是否为字符{textBox2!.Text = Encoding.GetEncoding( //字符转ASCII码"unicode").GetBytes(textBox1.Text)[0].ToString();}else{textBox2!.Text = string.Empty; //输出空字符串MessageBox.Show("请输入字母!", "提示!");//提示用户信息}}else{ MessageBox.Show("请输入字母!", "提示!");}}/// <summary>/// ASCII转字母/// </summary>private void Button2_Click(object? sender, EventArgs e){if (textBox3!.Text != string.Empty) //判断输入是否为空{if (int.TryParse( //将输入的字符转换为数字textBox3.Text, out int P_int_Num)){textBox4!.Text =((char)P_int_Num).ToString(); //ASCII码转为字符}else{MessageBox.Show( //如果输入不符合要求弹出提示框"请输入正确ASCII码值。", "错误!");}}else{MessageBox.Show("请输入ASCII!", "提示!"); } }}
}
三、生成效果