using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Example019
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                char chr = textBox1.Text[0];    //获得一个汉字字符
                byte[] gb2312_bt = Encoding.GetEncoding("gb2312").GetBytes(new Char[] { chr }); //使用gb2312编码方式获得字节序列
                int n = (int)gb2312_bt[0] << 8; //将字节序列的第一个字节向左移 8 位
                n += (int)gb2312_bt[1];         //第一字节左移8位后 和 第二字节相加 得到 汉字编码
                textBox2.Text = n.ToString();   //显示汉字编码
            }
            catch (Exception)
            {
                MessageBox.Show("请输入汉字字符!", "出现错误");
            }
        }
        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Text = "获取汉字编码值";
        }
    }
}
posted on 2011-10-17 01:06  C#_初学者  阅读(633)  评论(0编辑  收藏  举报