• 巧用位移运算符获取汉字编码值


    1.巧用位移运算符获取汉字编码值
    本实例实现时主要是利用移位运算符获取汉字编码值,先讲解一下位移运行符的相关概念。

    位移运算符分为左位移运算符“<<”和右位移运算符“>>”,分别用于向左和向右执行移位运算。对于X<<N或X>>N形式的运算,含义是将X向左或向右移动N位,X的类型可以是int、uint、long、ulong、bytes、byte、short和ushort。需要注意的是,byte、sbyte、short和ushort类型的值在进行位移操作后值的类型将自动转换成int类型。比如byte类型的1101向左位移4位后为11010000,位移操作后值的为208(自动转换成int类型)。

    PS:在进行位移运算时,当数值的二进制数每次向左移1位就相当于乘以2,当数值每次向右移1位就相当于除以2。

    获取汉字编码值的实现思路,众所周知,以GB2312标准编码的汉字,每个汉字都是用两个8位二进制字节储存的,我们只需要这两个字节第一个字节向左位移8位后(变成高8位),与第二个字符相加得到汉字编码值(int类型)。

    具体实现关键源代码如下:
    try
    {
    char chr = txt_chr.Text[0];//获得一个汉字字符
    byte[] gb2312_bt = //使用gb2312编码方式获得字节序列
    Encoding.GetEncoding("gb2312").GetBytes(new Char[] { chr });
    int n = (int)gb2312_bt[0] << 8;//将字节序列的第一个字节向左移8位
    n += (int)gb2312_bt[1];//第一个字节移8位后与第二个字节相加得到汉字编码
    txt_Num.Text = n.ToString();//显示汉字编码
    }
    catch (Exception)
    {
    MessageBox.Show(//异常提示信息
    "请输入汉字字符!", "出现错误!");
    }
    2.什么是闰年?
    公元年数可被4整除为闰年,但是正百的年数必须是可以被400整除的才是闰年。
    ushort P_usint_temp;//定义局部变量
    if (ushort.TryParse(//将输入字符串转换为数值
    txt_year.Text, out P_usint_temp))
    {
    MessageBox.Show(//输出计算结果
    (P_usint_temp % 4 == 0 && P_usint_temp % 100 != 0)//判断是否为闰年
    || P_usint_temp % 400 == 0 ? "输入的是闰年!" : "输入的不是闰年!",
    "提示!");
    }
    else
    {
    MessageBox.Show(//提示输入数值不正确
    "请输入正确数值!", "提示!");
    }
    3.使用checked关键字处理“溢出”错误
    byte bt_One, bt_Two;//定义两个byte类型变量
    if (byte.TryParse(//对两个byte类型变量赋值
    txt_Add_One.Text, out bt_One)
    && byte.TryParse(txt_Add_Two.Text, out bt_Two))
    {
    try
    {
    checked { bt_One += bt_Two; }//使用checke关键字判断是否有溢出
    txt_Result.Text = bt_One.ToString();//输出相加后的结果
    }
    catch (OverflowException ex)
    {
    MessageBox.Show(ex.Message,"出错!");//输出异常信息
    }
    }
    else
    {
    MessageBox.Show("请输入255以内的数字!");//输出错误信息
    }
    4.使用typeof关键字获取类的内部结构
    Type type = typeof(System.Int32);//获得int类型的Type对象
    foreach (MethodInfo method in type.GetMethods())//遍历string类中定义的所有公共方法
    {
    rtbox_text.AppendText(
    "方法名称:" + method.Name + Environment.NewLine);//输出方法名称
    foreach (ParameterInfo parameter in method.GetParameters())//遍历公共方法中所有参数
    {
    rtbox_text.AppendText(
    " 参数:" + parameter.Name + Environment.NewLine);//输出参数名称
    }
    }
    4.商品金额的大小写转换
    public string NumToChinese(string x)
    {
    //数字转换为中文后的数组
    string[] P_array_num = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
    //为数字位数建立一个位数组
    string[] P_array_digit = new string[] { "", "拾", "佰", "仟" };
    //为数字单位建立一个单位数组
    string[] P_array_units = new string[] { "", "万", "亿", "万亿" };
    string P_str_returnValue = ""; //返回值
    int finger = 0; //字符位置指针
    int P_int_m = x.Length % 4; //取模
    int P_int_k = 0;
    if (P_int_m > 0)
    P_int_k = x.Length / 4 + 1;
    else
    P_int_k = x.Length / 4;
    //外层循环,四位一组,每组最后加上单位: ",万亿,",",亿,",",万,"
    for (int i = P_int_k; i > 0; i--)
    {
    int P_int_L = 4;
    if (i == P_int_k && P_int_m != 0)
    P_int_L = P_int_m;
    //得到一组四位数
    string four = x.Substring(finger, P_int_L);
    int P_int_l = four.Length;
    //内层循环在该组中的每一位数上循环
    for (int j = 0; j < P_int_l; j++)
    {
    //处理组中的每一位数加上所在的位
    int n = Convert.ToInt32(four.Substring(j, 1));
    if (n == 0)
    {
    if (j < P_int_l - 1 && Convert.ToInt32(four.Substring(j + 1, 1)) > 0 && !P_str_returnValue.EndsWith(P_array_num[n]))
    P_str_returnValue += P_array_num[n];
    }
    else
    {
    if (!(n == 1 && (P_str_returnValue.EndsWith(P_array_num[0]) | P_str_returnValue.Length == 0) && j == P_int_l - 2))
    P_str_returnValue += P_array_num[n];
    P_str_returnValue += P_array_digit[P_int_l - j - 1];
    }
    }
    finger += P_int_L;
    //每组最后加上一个单位:",万,",",亿," 等
    if (i < P_int_k) //如果不是最高位的一组
    {
    if (Convert.ToInt32(four) != 0)
    //如果所有4位不全是0则加上单位",万,",",亿,"等
    P_str_returnValue += P_array_units[i - 1];
    }
    else
    {
    //处理最高位的一组,最后必须加上单位
    P_str_returnValue += P_array_units[i - 1];
    }
    }
    return P_str_returnValue;
    }

  • 相关阅读:
    Atitit  atiMail atiDns新特性 v2  q39
    Atitit  atiMail atiDns新特性 v2  q39
    Atitit.aticmd v4  新特性q39 添加定时器释放功能
    Atitit.aticmd v4  新特性q39 添加定时器释放功能
    Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39
    Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39
    Atitit.编程语言and 自然语言的比较and 编程语言未来的发展
    Atitit.编程语言and 自然语言的比较and 编程语言未来的发展
    atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException  v2 q31
    知也atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException  v2 q31无涯 - I
  • 原文地址:https://www.cnblogs.com/jxyZ/p/3281313.html
Copyright © 2020-2023  润新知