C#實(shí)現(xiàn)漢字轉(zhuǎn)區(qū)位碼的示例代碼
一、關(guān)于區(qū)位碼
1.區(qū)位碼定義
區(qū)位碼是一個(gè)4位的十進(jìn)制數(shù),每個(gè)區(qū)位碼都對應(yīng)著一個(gè)唯一的漢字,區(qū)位碼的前兩位叫做區(qū)碼,后兩位叫做位碼。
2.算法
首先通過Encoding對象的GetBytes方法得到漢字的字節(jié)數(shù)組,將字節(jié)數(shù)組的第一位和第二位分別轉(zhuǎn)換為整型數(shù)值,然后將得到的兩個(gè)整型數(shù)值分別減160后轉(zhuǎn)換為字符串,連接兩個(gè)字符串就組成了漢字區(qū)位碼。
string P_Chinese ="科"; byte[] P_bt_array =Encoding.Default.GetBytes(Chinese); short front =(short)(P_bt_array[0]-'\0'); //將字節(jié)數(shù)組的第一位轉(zhuǎn)換成short類型 short back =(short)(P_bt_array[1]-'\0'); //將字節(jié)數(shù)組的第二位轉(zhuǎn)換成short類型 string P_Result =(front -160).ToString()+(back-160).ToString();
Encoding對象的GetBytes方法提供了多個(gè)重載,可以接收字符串、字符數(shù)組等對象。
可以使用FileStream對象將字節(jié)數(shù)組寫入文件。
使用Encoding對象的GetBytes方法可以得到字符串對象的字節(jié)數(shù)組,現(xiàn)在可以創(chuàng)建一個(gè)FileStream對象,方便地將字節(jié)數(shù)組寫入文件中。同樣地,可以從文件中讀取字節(jié)數(shù)組,然后調(diào)用Encoding對象的GetString方法將字符數(shù)組轉(zhuǎn)換為字符串。
二、實(shí)例
//漢字與區(qū)位碼的轉(zhuǎn)換 using System.Text; namespace _037 { public partial class Form1 : Form { private GroupBox? groupBox1; private TextBox? textBox2; private Button? button1; private TextBox? textBox1; private Label? label1; public Form1() { InitializeComponent(); Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // textBox2 // textBox2 = new TextBox { Location = new Point(102, 57), Name = "textBox2", Size = new Size(100, 23), TabIndex = 3 }; // // button1 // button1 = new Button { Location = new Point(21, 57), Name = "button1", Size = new Size(75, 23), TabIndex = 2, Text = "轉(zhuǎn)區(qū)位碼", UseVisualStyleBackColor = true }; button1.Click += Button1_Click; // // textBox1 // textBox1 = new TextBox { Location = new Point(102, 28), Name = "textBox1", Size = new Size(100, 23), TabIndex = 1 }; // // label1 // label1 = new Label { AutoSize = true, Location = new Point(21, 34), Name = "label1", Size = new Size(68, 17), TabIndex = 0, Text = "輸入漢字:" }; // // groupBox1 // groupBox1 = new GroupBox { Location = new Point(12, 12), Name = "groupBox1", Size = new Size(220, 102), TabIndex = 0, TabStop = false, Text = "漢字轉(zhuǎn)區(qū)位碼" }; groupBox1.SuspendLayout(); groupBox1.Controls.Add(textBox2); groupBox1.Controls.Add(button1); groupBox1.Controls.Add(textBox1); groupBox1.Controls.Add(label1); // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(244, 126); Controls.Add(groupBox1); Name = "Form1"; StartPosition = FormStartPosition.CenterScreen; Text = "漢字轉(zhuǎn)區(qū)位碼"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); } private void Button1_Click(object? sender, EventArgs e) { if (textBox1!.Text != string.Empty) //判斷輸入是否為空 { try { textBox2!.Text = //得到漢字區(qū)位碼信息 GetCode(textBox1.Text); } catch (IndexOutOfRangeException ex) { MessageBox.Show( //使用消息對話框提示異常信息 ex.Message + "請輸入正確的漢字", "出錯(cuò)!"); } } else { MessageBox.Show("請輸入正確的漢字", "提醒!"); } } /// <summary> /// 得到漢字區(qū)位碼方法 /// </summary> /// <param name="strChinese">漢字字符</param> /// <returns>返回漢字區(qū)位碼</returns> public static string GetCode(string Chinese) { byte[] byte_array = Encoding.Default.GetBytes(Chinese); //字符串轉(zhuǎn)Byte數(shù)組 int front = (short)(byte_array[0] - '\0'); //將字節(jié)數(shù)組的第一位轉(zhuǎn)換成short類型 int back = (short)(byte_array[1] - '\0'); //將字節(jié)數(shù)組的第二位轉(zhuǎn)換成short類型 return (front - 160).ToString() + (back - 160).ToString();//計(jì)算并返回區(qū)位碼 } } }
三、生成效果
四、程序中的知識點(diǎn)
1.byte[] GetBytes(string s)
用途:字符串轉(zhuǎn)Byte數(shù)組
public virtual byte[] GetBytes(string s) { if (s == null) { throw new ArgumentNullException("s", Environment.GetResourceString("ArgumentNull_String")); } int byteCount = GetByteCount(s); byte[] array = new byte[byteCount]; int bytes = GetBytes(s, 0, s.Length, array, 0); return array; }
//使用UTF-8的字符集,將字符串轉(zhuǎn)換為字節(jié)數(shù)組 byte[]utf8 =Encoding.UTF8.GetBytes(str);
2.字節(jié)數(shù)組轉(zhuǎn)short類型
到此這篇關(guān)于C#實(shí)現(xiàn)漢字轉(zhuǎn)區(qū)位碼的示例代碼的文章就介紹到這了,更多相關(guān)C#漢字轉(zhuǎn)區(qū)位碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#對圖片進(jìn)行馬賽克處理可控制模糊程度的實(shí)現(xiàn)代碼
本文通過實(shí)例代碼給大家介紹了C#對圖片進(jìn)行馬賽克處理可控制模糊程度的實(shí)現(xiàn)方法,代碼超簡單,具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05WPF實(shí)現(xiàn)控件拖動(dòng)的示例代碼
這篇文章主要介紹了WPF實(shí)現(xiàn)控件拖動(dòng)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08C#判斷當(dāng)前程序是否通過管理員運(yùn)行的方法
這篇文章主要介紹了C#判斷當(dāng)前程序是否通過管理員運(yùn)行的方法,可通過非常簡單的系統(tǒng)函數(shù)調(diào)用實(shí)現(xiàn)對當(dāng)前程序是否通過管理員運(yùn)行進(jìn)行判定,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11使用Spire.Barcode程序庫生成二維碼的實(shí)例解析
這篇文章主要介紹了使用Spire.Barcode程序庫生成二維碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12c#模擬平拋運(yùn)動(dòng)動(dòng)畫的方法詳解
本篇文章是對使用c#模擬平拋運(yùn)動(dòng)動(dòng)畫的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06