C#中如何获得汉字的区位码?

发布时间 2023-10-14 09:55:24作者: 一路探索者

1、区位码转换为国标码方法如下:

国标码高位字节=(区号)H+20H

国标码低位字节=(位号)H+20H

 

2、国标码转换为机内码方法如下:

机内码高位字节=(国标码)H+80H

机内码低位字节=(国标码)H+80H

 

3、区位码转换为机内码方法如下:

可见,区位码到机内码加了两次:20H + 80H = 32 + 128 = 160 =  A0H

因此,区位码转换机内码的方法,是加160。

机内码高位字节=(区号)H+A0H

机内码低位字节=(位号)H+A0H

 

反之,机内码转换区位码的方法,是减160。

注意:计算机使用的字符都为机内码!

 

要获取字符对应的为字节序列。可以通过Encoding类的Default属性和GetBytes( )方法来实现。Encoding类的Default属性为Encoding对象。

GetBytes( )方法声明如下:

public virtual byte[] GetBytes(

       char[] chars

)

 

public virtual byte[] GetBytes(

       string s

)

 

public virtual byte[] GetBytes(

       char[] chars,

       int index,

       int count

)

 

[CLSCompliantAttribute(false)]

[ComVisibleAttribute(false)]

public virtual int GetBytes(

       char* chars,

       int charCount,

       byte* bytes,

       int byteCount

)

 

public abstract int GetBytes(

       char[] chars,

       int charIndex,

       int charCount,

       byte[] bytes,

       int byteIndex

)

参数

chars

类型:System.Char[]

包含要编码的字符集的字符数组。

charIndex

类型:System.Int32

第一个要编码的字符的索引。

charCount

类型:System.Int32

要编码的字符的数目。

bytes

类型:System.Byte[]

要包含所产生的字节序列的字节数组。

byteIndex

类型:System.Int32

开始写入所产生的字节序列的索引位置。

返回值

类型:System.Int32

写入 bytes 的实际字节数。

 

public virtual int GetBytes(

       string s,

       int charIndex,

       int charCount,

       byte[] bytes,

       int byteIndex

)

参数

s

类型:System.String

包含要编码的字符集的 String。

charIndex

类型:System.Int32

第一个要编码的字符的索引。

charCount

类型:System.Int32

要编码的字符的数目。

bytes

类型:System.Byte[]

要包含所产生的字节序列的字节数组。

byteIndex

类型:System.Int32

开始写入所产生的字节序列的索引位置。

返回值

类型:System.Int32

写入 bytes 的实际字节数。

 

      为了便于理解,以下提供区位码部分截图:

(1)、01区

01 0 1 2 3 4 5 6 7 8 9
0     、 。 · ˉ ˇ ¨ 〃 々
1 — ~ ‖ … ‘ ’ “ ” 〔 〕
2 〈 〉 《 》 「 」 『 』 〖 〗
3 【 】 ± × ÷ ∶ ∧ ∨ ∑ ∏
4 ∪ ∩ ∈ ∷ √ ⊥ ∥ ∠ ⌒ ⊙
5 ∫ ∮ ≡ ≌ ≈ ∽ ∝ ≠ ≮ ≯
6 ≤ ≥ ∞ ∵ ∴ ♂ ♀ ° ′ ″
7 ℃ $ ¤ ¢ £ ‰ § № ☆ ★
8 ○ ● ◎ ◇ ◆ □ ■ △ ▲ ※
9 → ← ↑ ↓ 〓
 

(2)、02区

02 0 1 2 3 4 5 6 7 8 9
0   ⅰ ⅱ ⅲ ⅳ ⅴ ⅵ ⅶ ⅷ ⅸ
1 ⅹ       ⒈ ⒉ ⒊
2 ⒋ ⒌ ⒍ ⒎ ⒏ ⒐ ⒑ ⒒ ⒓ ⒔
3 ⒕ ⒖ ⒗ ⒘ ⒙ ⒚ ⒛ ⑴ ⑵ ⑶
4 ⑷ ⑸ ⑹ ⑺ ⑻ ⑼ ⑽ ⑾ ⑿ ⒀
5 ⒁ ⒂ ⒃ ⒄ ⒅ ⒆ ⒇ ① ② ③
6 ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩   ㈠
7 ㈡ ㈢ ㈣ ㈤ ㈥ ㈦ ㈧ ㈨ ㈩ 
8  Ⅰ Ⅱ Ⅲ Ⅳ Ⅴ Ⅵ Ⅶ Ⅷ Ⅸ
9 Ⅹ Ⅺ Ⅻ  

 

(3)、17区

 

 

17 0 1 2 3 4 5 6 7 8 9
0   薄 雹 保 堡 饱 宝 抱 报 暴
1 豹 鲍 爆 杯 碑 悲 卑 北 辈 背
2 贝 钡 倍 狈 备 惫 焙 被 奔 苯
3 本 笨 崩 绷 甭 泵 蹦 迸 逼 鼻
4 比 鄙 笔 彼 碧 蓖 蔽 毕 毙 毖
5 币 庇 痹 闭 敝 弊 必 辟 壁 臂
6 避 陛 鞭 边 编 贬 扁 便 变 卞
7 辨 辩 辫 遍 标 彪 膘 表 鳖 憋
8 别 瘪 彬 斌 濒 滨 宾 摈 兵 冰
9 柄 丙 秉 饼 炳

 

      本例的关键代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text; //引用Encoding

using System.Windows.Forms;

 

private void button1_Click(object sender, EventArgs e)

{

    try

    {

        if (textBox1.Text == "")

        {

            MessageBox.Show("必须先输入汉字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            textBox1.Focus();

        }

        else

        {

            byte[] array = new byte[2];

            array = System.Text.Encoding.Default.GetBytes("" + textBox1.Text.Trim() + "");

            int front =(short)(array[0]-'\0');

            int back =(short)(array[1]-'\0');

 

            //计算机使用的字符都为机内码!

            //即Encoding类的GetBytes()返回值对应机内码

            //机内码转换区位码的方法,是减160

            int front1 = front - 160;

            int back1 = back - 160;

 

            string strFront1, strBack1;

            if (front1 > 9)

                strFront1 = Convert.ToString(front1);

            else

                strFront1 = "0" + Convert.ToString(front1);

            if (back1 > 9)

                strBack1 = Convert.ToString(back1);

            else

                strBack1 = "0" + Convert.ToString(back1);

 

            textBox2.Text = strFront1 + strBack1;

        }

    }

    catch

    {

        MessageBox.Show("输入有误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        textBox1.Text = "";

        textBox1.Focus();

    }

}