<pre name="code" class="csharp">private void button1_Click(object sender, EventArgs e)
{
int x = 0;
string add="";
if (textBox1.Text == "")
MessageBox.Show("不允许输入空值");
else
{
foreach (char a in textBox1.Text)
{
x+= a;
add += x.ToString()+" ";
x = 0;
}
}
textBox2.Text = add;
}
简单的转换 转换后的数每一个都加空格来区分
private void button1_Click(object sender, EventArgs e)
{
string asc = textBox1.Text;
string[] word = asc.Split(new string[] { " ", " " }, StringSplitOptions.RemoveEmptyEntries);
;//split 截取每个汉子代表的ascII 并去掉空格
ArrayList List = new ArrayList();//动态长度的数组
int[] num =new int[] { };
string tx="";
for (int i = 0; i < word.Length; i++)
{
List.Add(Convert.ToInt32(word[i]));//填充动态数组
}
num = (int[])List.ToArray(typeof(int));//转换为所需类型的数组
for (int j = 0; j < num.Length; j++)
{
tx += ((char)num[j]).ToString();//ASCII转换为文本
}
textBox2.Text = tx;
}
本文介绍了使用C#实现字符到ASCII码及反向转换的方法。通过两个示例,展示了如何将文本框中的字符逐一转换为其对应的ASCII码,并将ASCII码还原成原始字符。适用于初学者了解基本的字符串操作。
1万+

被折叠的 条评论
为什么被折叠?



