1. 程式人生 > 其它 >C#精彩程式設計200例-第4例-字母-ASCII碼轉化

C#精彩程式設計200例-第4例-字母-ASCII碼轉化

技術標籤:C# and Halconc#

C#精彩程式設計200例-第4例-字母-ASCII碼轉化

1. 效果

在這裡插入圖片描述

2. 實現程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Demo4_字母與ASCII碼互相轉換 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public bool IsLetter(char c) { if (c == char.MinValue) return false; if ((c >= 'a' && c <=
'z') || (c >= 'A' && c <= 'Z')) { return true; } return false; } private void btnStringToASCII_Click(object sender, EventArgs e) { if (this.txbString.Text == string.Empty) return; char
c = this.txbString.Text[0]; if (!IsLetter(c)) { txbToASCII.Text = string.Empty; MessageBox.Show("請輸入字母!", "提示!"); } Encoding encode = Encoding.GetEncoding("unicode"); byte[] bytes = encode.GetBytes(this.txbString.Text); this.txbToASCII.Text = bytes[0].ToString(); } private void btnASCIIToString_Click(object sender, EventArgs e) { if (this.txbASCII.Text == string.Empty) return; int number; bool bIsNumber = int.TryParse(this.txbASCII.Text, out number); if (bIsNumber) { this.txbToString.Text = ((char)number).ToString(); } else { this.txbToString.Text = string.Empty; MessageBox.Show("請輸入ASCII!", "提示!"); } } } }