C#實現(xiàn)字母與ASCII碼互相轉(zhuǎ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)
到此這篇關于C#實現(xiàn)字母與ASCII碼互相轉(zhuǎn)換的文章就介紹到這了,更多相關C#字母與ASCII碼互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#利用OLEDB實現(xiàn)將DataTable寫入Excel文件中
這篇文章主要為大家詳細介紹了C#如何利用OLEDB實現(xiàn)將DataTable寫入Excel文件中,文中的示例代碼簡潔易懂,具有一定的借鑒價值,需要的可以參考一下2023-02-02
C# StreamReader類實現(xiàn)讀取文件的方法
這篇文章主要介紹了C# StreamReader類實現(xiàn)讀取文件的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
C#實現(xiàn)PDF文檔自動化生成的開發(fā)實戰(zhàn)
在現(xiàn)代軟件應用中,PDF文檔因其跨平臺、內(nèi)容固定性強以及易于分享的特性,扮演著不可或缺的角色,本文將深入探討如何利用C#強大的能力,結(jié)合一款功能豐富的PDF處理庫,實現(xiàn)PDF文檔的自動化生成,需要的朋友可以參考下2026-01-01
英雄聯(lián)盟輔助lol掛機不被踢的方法(lol掛機腳本)
lol掛機不會被踢,調(diào)用API設置鼠標位置并模擬鼠標右鍵讓人物走動2013-12-12
Unity之Luaframework框架lua調(diào)用C#方法
這篇文章主要介紹了Unity之Luaframework框架lua調(diào)用C#方法,在這里需要寫一個C#腳本,腳本里寫方法需要在lua中調(diào)用,具體實例代碼參考下本文吧2021-09-09

