C#實(shí)現(xiàn)字母與ASCII碼互相轉(zhuǎn)換
一、關(guān)于ASCII及與字符互轉(zhuǎn)
ASCII(American Standard Code for Information Interchange,美國信息互換標(biāo)準(zhǔn)代碼)是基于拉丁字母的編碼系統(tǒng),也是現(xiàn)今最通用的單字節(jié)編碼系統(tǒng)。在程序設(shè)計(jì)中,可以方便地將字母轉(zhuǎn)換為ASCⅡ碼,也可以將ASCII碼方便地轉(zhuǎn)換為字母。
1.主要用到Encoding對(duì)象的GetBytes方法
Encoding對(duì)象的GetBytes方法接收一個(gè)字符串或字符數(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對(duì)象,然后調(diào)用Encoding對(duì)象的GetBytes方法,該方法接收一個(gè)字符串或字符數(shù)組作為參數(shù),最后GetBytes方法會(huì)返回字節(jié)數(shù)組對(duì)象,可以根據(jù)字節(jié)數(shù)組的第0個(gè)索引來得到字符串中第一個(gè)字母的ASCⅡ碼。
2.Char顯式轉(zhuǎn)換為數(shù)值類型得到ASCⅡ
字符Char是值類型,它總是表示成16位Unicode代碼值。
現(xiàn)在已經(jīng)了解到Char是值類型,如果將Char顯式轉(zhuǎn)換為數(shù)值類型,可以方便地得到ASCⅡ碼值。相反,如果將ASCⅡ碼數(shù)值強(qiáng)制轉(zhuǎn)換為Char,將會(huì)得到一個(gè)Char對(duì)象。
二、實(shí)例
// 字符與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ù)組的范圍(在第一個(gè)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("請(qǐng)輸入字母!", "提示!");//提示用戶信息 } } else { MessageBox.Show("請(qǐng)輸入字母!", "提示!"); } } /// <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( //如果輸入不符合要求彈出提示框 "請(qǐng)輸入正確ASCII碼值。", "錯(cuò)誤!"); } } else { MessageBox.Show("請(qǐng)輸入ASCII!", "提示!"); } } } }
三、生成效果
四、程序中的一些知識(shí)點(diǎn)
1.IsLetterOrDigit()
2.GetBytes()
3.TryParse(string, out int)
到此這篇關(guān)于C#實(shí)現(xiàn)字母與ASCII碼互相轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)C#字母與ASCII碼互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity 按鈕事件封裝操作(EventTriggerListener)
這篇文章主要介紹了Unity 按鈕事件封裝操作(EventTriggerListener),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04c#使用DotNetZip封裝類操作zip文件(創(chuàng)建/讀取/更新)實(shí)例
DotnetZip是一個(gè)開源類庫,支持.NET的任何語言,可很方便的創(chuàng)建,讀取,和更新zip文件。而且還可以使用在.NETCompact Framework中。2013-11-11C# wpf 實(shí)現(xiàn)窗口任意區(qū)域點(diǎn)擊拖動(dòng)
在wpf要實(shí)現(xiàn)此功能簡(jiǎn)單形式還是比較容易的,但是有一些細(xì)節(jié)需要專門處理,比如與按鈕的點(diǎn)擊事件沖突問題,解決事件沖突問題后拖動(dòng)的靈敏度,可復(fù)用性等,這篇文章主要介紹了C# wpf 實(shí)現(xiàn)窗口任意區(qū)域點(diǎn)擊拖動(dòng),需要的朋友可以參考下2024-03-03C# Winform實(shí)現(xiàn)自定義漂亮的通知效果
這篇文章主要介紹了C# Winform實(shí)現(xiàn)自定義漂亮的通知效果,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08C#實(shí)現(xiàn)的ZPL條碼打印類完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的ZPL條碼打印類,結(jié)合實(shí)例形式詳細(xì)分析了C#實(shí)現(xiàn)條碼打印的原理與使用方法,代碼注釋中備有詳盡的說明,便于理解使用,需要的朋友可以參考下2016-06-06c# DataView.ToTable()方法 去除表的重復(fù)項(xiàng)問題
這篇文章主要介紹了c# DataView.ToTable()方法 去除表的重復(fù)項(xiàng)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12