C#實現(xiàn)字母與ASCII碼互相轉(zhuǎn)換
一、關(guān)于ASCII及與字符互轉(zhuǎn)
ASCII(American Standard Code for Information Interchange,美國信息互換標準代碼)是基于拉丁字母的編碼系統(tǒng),也是現(xiàn)今最通用的單字節(jié)編碼系統(tǒng)。在程序設計中,可以方便地將字母轉(zhuǎn)換為ASCⅡ碼,也可以將ASCII碼方便地轉(zhuǎn)換為字母。
1.主要用到Encoding對象的GetBytes方法
Encoding對象的GetBytes方法接收一個字符串或字符數(shù)組作為參數(shù),最后返回字節(jié)數(shù)組,可以根據(jù)字節(jié)數(shù)組得到字母的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靜態(tài)方法得到Encoding對象,然后調(diào)用Encoding對象的GetBytes方法,該方法接收一個字符串或字符數(shù)組作為參數(shù),最后GetBytes方法會返回字節(jié)數(shù)組對象,可以根據(jù)字節(jié)數(shù)組的第0個索引來得到字符串中第一個字母的ASCⅡ碼。
2.Char顯式轉(zhuǎn)換為數(shù)值類型得到ASCⅡ
字符Char是值類型,它總是表示成16位Unicode代碼值。
現(xiàn)在已經(jīng)了解到Char是值類型,如果將Char顯式轉(zhuǎn)換為數(shù)值類型,可以方便地得到ASCⅡ碼值。相反,如果將ASCⅡ碼數(shù)值強制轉(zhuǎn)換為Char,將會得到一個Char對象。
二、實例
// 字符與ASCII相互轉(zhuǎn)換
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轉(zhuǎn)字符",
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 = "字符轉(zhuǎn)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相互轉(zhuǎn)換"
};
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互轉(zhuǎn)";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
}
/// <summary>
/// 字母轉(zhuǎn)ASCII
/// 注釋掉的部分異常:
/// Index was outside of the bounds of the array
/// 未處理的異常:System.IndexOutOfRangeException:索引超出數(shù)組的范圍(在第一個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( //字符轉(zhuǎn)ASCII碼
"unicode").GetBytes(textBox1.Text)[0].ToString();
}
else
{
textBox2!.Text = string.Empty; //輸出空字符串
MessageBox.Show("請輸入字母!", "提示!");//提示用戶信息
}
}
else
{
MessageBox.Show("請輸入字母!", "提示!");
}
}
/// <summary>
/// ASCII轉(zhuǎn)字母
/// </summary>
private void Button2_Click(object? sender, EventArgs e)
{
if (textBox3!.Text != string.Empty) //判斷輸入是否為空
{
if (int.TryParse( //將輸入的字符轉(zhuǎn)換為數(shù)字
textBox3.Text, out int P_int_Num))
{
textBox4!.Text =
((char)P_int_Num).ToString(); //ASCII碼轉(zhuǎn)為字符
}
else
{
MessageBox.Show( //如果輸入不符合要求彈出提示框
"請輸入正確ASCII碼值。", "錯誤!");
}
}
else
{
MessageBox.Show("請輸入ASCII!", "提示!");
}
}
}
}三、生成效果



四、程序中的一些知識點
1.IsLetterOrDigit()
2.GetBytes()
3.TryParse(string, out int)
到此這篇關(guān)于C#實現(xiàn)字母與ASCII碼互相轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)C#字母與ASCII碼互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity 按鈕事件封裝操作(EventTriggerListener)
這篇文章主要介紹了Unity 按鈕事件封裝操作(EventTriggerListener),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
c#使用DotNetZip封裝類操作zip文件(創(chuàng)建/讀取/更新)實例
DotnetZip是一個開源類庫,支持.NET的任何語言,可很方便的創(chuàng)建,讀取,和更新zip文件。而且還可以使用在.NETCompact Framework中。2013-11-11
c# DataView.ToTable()方法 去除表的重復項問題
這篇文章主要介紹了c# DataView.ToTable()方法 去除表的重復項問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

