• unicode编码、字符的转换和得到汉字的区位码


    一:unicode编码、字符的转换截图

    二:unicode编码、字符的转换代码

    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 ASCII
    {
        public partial class Frm_Main : Form
        {
            public Frm_Main()
            {
                InitializeComponent();
            }
    
            private void btn_ToASCII_Click(object sender, EventArgs e)
            {
                if (txt_char.Text != string.Empty)//判断输入是否为空
                {
                    if (Encoding.GetEncoding("unicode").//判断输入是否为字母,数字,标点符号,等
                        GetBytes(new char[] { txt_char.Text[0] })[1] == 0)//编码的第1位为0
                    {
                        txt_ASCII.Text = Encoding.GetEncoding(//得到字符的ASCII码值
                            "unicode").GetBytes(txt_char.Text)[0].ToString();
                        MessageBox.Show(txt_char.Text[0].ToString());
                    }
                    else
                    {
                        txt_ASCII.Text = string.Empty;//输出空字符串
                        MessageBox.Show("请输入字母!","提示!");//提示用户信息
                    }
                }
            }
            private void btn_ToChar_Click(object sender, EventArgs e)
            {
                if (txt_ASCII2.Text != string.Empty)//判断输入是否为空
                {
                    int P_int_Num;//定义整型局部变量
                    if (int.TryParse(//将输入的字符转换为数字
                        txt_ASCII2.Text, out P_int_Num))
                    {
                        txt_Char2.Text =
                            ((char)P_int_Num).ToString();//将ASCII码转换为字符
                    }
                    else
                    {
                        MessageBox.Show(//如果输入不符合要求弹出提示框
                            "请输入正确ASCII码值。", "错误!");
                    }
                }
                string P_str_temp = "abc";
                string P_str = Encoding.GetEncoding("unicode").GetBytes(new char[] { P_str_temp[0] })[0].ToString();
            }
        }
    }

     三:得到汉字的区位码

    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 ChineseCode
    {
        public partial class Frm_Main : Form
        {
            public Frm_Main()
            {
                InitializeComponent();
            }
    
            private void btn_Get_Click(object sender, EventArgs e)
            {
                if (txt_Chinese.Text != string.Empty)//判断输入是否为空
                {
                    try
                    {
                        txt_Num.Text = //得到汉字区位码信息
                            getCode(txt_Chinese.Text);
                    }
                    catch (IndexOutOfRangeException ex)
                    {
                        MessageBox.Show(//使用消息对话框提示异常信息
                            ex.Message + "请输入正确的汉字", "出错!");
                    }
                }
            }
            /// <summary>
            /// 得到汉字区位码方法
            /// </summary>
            /// <param name="strChinese">汉字字符</param>
            /// <returns>返回汉字区位码</returns>
            public string getCode(string Chinese)
            {//(' 区位码是与汉字一一对应的编码,用四位数字表示,
                //前两位从01 到94称区码,后两位从01到94称位码。 一个汉字的前一半是 ASCⅡ码为“160+区码”的字符,
                //后一半是ASCⅡ码为“160+ 位码”的字符。'例如:“刘”的区位码是 3385,
                //其意为区码33位码85,它是由ASCⅡ码为160+33=193和160+85=245的两个字符组成。
     
                byte[] P_bt_array = Encoding.Default.GetBytes(Chinese);//得到汉字的Byte数组
                int front = (short)(P_bt_array[0] - '');//将字节数组的第一位转换成short类型,这里(short)code[0]也是可以的
                int back = (short)(P_bt_array[1] - '');//将字节数组的第二位转换成short类型
                return (front - 160).ToString() + (back - 160).ToString();//计算并返回区位码
            }
        }
    }
  • 相关阅读:
    Ext.dataGroupingStore/JsonStore/SimpleStore
    转:LinQ操作汇总(From CSharpSamples)
    XSLT教程 比较全的
    使用ASP.Net Forms模式实现WebService身份验证
    关于DataRow的RowState和RowVersion
    C#日志工具汇总
    转 Using log4net,
    js//初始话日期
    两个数据库表的连接 查询
    ExtJS入门之三 查询
  • 原文地址:https://www.cnblogs.com/hongmaju/p/3734171.html
Copyright © 2020-2023  润新知