获取汉字首字母

  /// <summary>
  /// 获取汉字首字母。
  /// </summary>
  private string GetPYChar(char c)
  {
   int tmp= 65536 + GetASCII(c);
   if(tmp>=45217 && tmp<=45252)
    return "A";
   else if (tmp>=45253 && tmp<=45760)
    return "B";
   else if(tmp>=45761 && tmp<=46317)
    return "C";
   else if(tmp>=46318 && tmp<=46825)
    return "D";
   else if(tmp>=46826 && tmp<=47009) 
    return "E";
   else if(tmp>=47010 && tmp<=47296) 
    return "F";
   else if(tmp>=47297 && tmp<=47613) 
    return "G";
   else if(tmp>=47614 && tmp<=48118)
    return "H";
   else if(tmp>=48119 && tmp<=49061)
    return "J";
   else if(tmp>=49062 && tmp<=49323) 
    return "K";
   else if(tmp>=49324 && tmp<=49895) 
    return "L";
   else if(tmp>=49896 && tmp<=50370) 
    return "M";
   else if(tmp>=50371 && tmp<=50613) 
    return "N";
   else if(tmp>=50614 && tmp<=50621) 
    return "O";
   else if(tmp>=50622 && tmp<=50905)
    return "P";
   else if(tmp>=50906 && tmp<=51386) 
    return "Q";
   else if(tmp>=51387 && tmp<=51445) 
    return "R";
   else if(tmp>=51446 && tmp<=52217) 
    return "S";
   else if(tmp>=52218 && tmp<=52697) 
    return "T";
   else if(tmp>=52698 && tmp<=52979) 
    return "W";
   else if(tmp>=52980 && tmp<=53640) 
    return "X";
   else if(tmp>=53689 && tmp<=54480) 
    return "Y";
   else if(tmp>=54481 && tmp<=62289)
    return "Z";
   else //如果不是中文,则不处理
    return c.ToString();
   
  }

  /// <summary>
  /// 是否为双字节字符。
  /// </summary>
  public static bool IsTwoBytesChar(char chr)
  {
   string str =chr.ToString();
   // 使用中文支持编码
   Encoding ecode = Encoding.GetEncoding("GB18030");
   if (ecode.GetByteCount(str) == 2)
   {
    return true;
   }
   else
   {
    return false;
   }
  }

  /// <summary>
  /// 得到字符的ASCII码
  /// </summary>
  public static int GetASCII(char chr)
  {
   Encoding ecode = Encoding.GetEncoding("GB18030");
   Byte[] codeBytes = ecode.GetBytes(chr.ToString());
   if ( IsTwoBytesChar(chr) )
   {
    // 双字节码为高位乘256,再加低位
    // 该为无符号码,再减65536
    return (int)codeBytes[0] * 256 + (int)codeBytes[1] - 65536;
   }
   else
   {
    return (int)codeBytes[0];
   }
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值