haoxiaobo

从C到C++又到.net, 有一些心得, 和大家交流下...
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

整理出来的得到汉字拼音的代码.

Posted on 2005-01-11 11:47  HAL9000  阅读(1168)  评论(2编辑  收藏  举报

using System;
using System.Text;


namespace HXBTools.Util
{
 /// <summary>
 ///
 /// </summary>
 public class py
 {
  public py()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  public static char GetPyChar(char c)
  {
   return GetPyChar("" + (char)c);
  }

  public static char GetPyChar(string strName)
  {
   short code;
   if (strName == "" )
    return ' ';

   code = GetGB2312Code(strName);
   
   // 英文字符区
   if( code >= 32 && code <= 126 ) return char.ToUpper(strName[0]);
   // 全角字符区
   if(strName[0] >= 'A' && strName[0] <= 'Z')
    return (char)(strName[0] - 'A' + 'A');

   if(strName[0] >= 'a' && strName[0] <= 'z')
    return (char)(strName[0] - 'a' + 'A');

   if(strName[0] >= '0' && strName[0] <= '9')
    return (char)(strName[0] - '0' + '0');

   //拼音顺序区.   
   int tmp = 65536 + code;
   
   if( (tmp >= 45217 && tmp <= 45252) ) return  'A';
   if( (tmp >= 45253 && tmp <= 45760) ) return  'B';
   if( (tmp >= 45761 && tmp <= 46317) ) return  'C';
   if( (tmp >= 46318 && tmp <= 46825) ) return  'D';
   if( (tmp >= 46826 && tmp <= 47009) ) return  'E';
   if( (tmp >= 47010 && tmp <= 47296) ) return  'F';
   if( (tmp >= 47297 && tmp <= 47613) ) return  'G';
   if( (tmp >= 47614 && tmp <= 48118) ) return  'H';
   if( (tmp >= 48119 && tmp <= 49061) ) return  'J';
   if( (tmp >= 49062 && tmp <= 49323) ) return  'K';
   if( (tmp >= 49324 && tmp <= 49895) ) return  'L';
   if( (tmp >= 49896 && tmp <= 50370) ) return  'L';
   if( (tmp >= 50371 && tmp <= 50613) ) return  'N';
   if( (tmp >= 50614 && tmp <= 50621) ) return  'O';
   if( (tmp >= 50622 && tmp <= 50905) ) return  'P';
   if( (tmp >= 50906 && tmp <= 51386) ) return  'Q';
   if( (tmp >= 51387 && tmp <= 51445) ) return  'R';
   if( (tmp >= 51446 && tmp <= 52217) ) return  'S';
   if( (tmp >= 52218 && tmp <= 52697) ) return  'T';
   if( (tmp >= 52698 && tmp <= 52979) ) return  'W';
   if( (tmp >= 52980 && tmp <= 53640) ) return  'X';
   if( (tmp >= 53689 && tmp <= 54480) ) return  'Y';
   if( (tmp >= 54481 && tmp <= 55289) ) return  'Z';

   // 转换GB2312内码到区位以检查难检字
   int iQh = ((code >> 8) & 0x00ff) - 160;
   int iQl = (code & 0x000000ff) - 160;
   if (iQl < 0)
   {
    return ' '; // 那些不在gb2312字符集里的字们目前还没有办法。
   }
   int iQ = iQh * 100 + iQl;
   if (iQ >=5601 && iQ <= 8794) // 区位的难检字区
   {
    int pos = ( iQh - 56 ) * 94 + iQl - 1;
    if (pos < 0 || pos >= sCodeData.Length)
    {
     return ' ';
    }
    return sCodeData[pos - 1];
   }
   return ' ';
  }

  /// <summary>
  /// 难检字码表.
  /// </summary>
  private static string sCodeData =
   "CJWGNSPGCENEGYPBTWXZDXYKYGTPJNMJQMBSGZSCYJSYYFPGGBZGYDYWJKGALJSWKBJQHYJWPDZLSGMR"
   + "YBYWWCCGZNKYDGTTNGJEYEKZYDCJNMCYLQLYPYQBQRPZSLWBDGKJFYXJWCLTBNCXJJJJCXDTQSQZYCDXXHGCKBPHFFSS"
   + "PYBGMXJBBYGLBHLSSMZMPJHSOJNGHDZCDKLGJHSGQZHXQGKEZZWYMCSCJNYETXADZPMDSSMZJJQJYZCJJFWQJBDZBJGD"
   + "NZCBWHGXHQKMWFBPBQDTJJZKQHYLCGXFPTYJYYZPSJLFCHMQSHGMMXSXJPKDCMBBQBEFSJWHWWGCKPYLQBGLDLCCTNMA"
   + "EDDKSJNGKCSGXLHZAYBDBTSDKDYLHGYMYLCXPYCJNDQJWXQXFYYFJLEJBZRWCCQHQCSBZKYMGPLBMCRQCFLNYMYQMSQT"
   + "RBCJTH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RPPXJCGLZCSHLTOLJNMDDDLNGKAQAKGJGYHHEZNMSHRPHQQJCHGMFPRXCJG"
   + "DYCHGHLYRZQLCNGJNZSQDKQJYMSZSWLCFQJQXGBGGXMDJWLMCRNFKKFSYYLJBMQAMMMYCCTBSHCPTXXZZSMPHFSHMCLM"
   + "LDJFYQXSDYJDJJZZHQPDSZGLSSJBCKBXYQZJSGPSXJZQZNQTBDKWXJKHHGFLBCSMDLDGDZDBLZKYCQNNCSYBZBFGLZZX"
   + "SWMSCCMQNJQSBDQSJTXXMBLDXCCLZSHZCXRQJGJYLXZFJPHYMZQQYDFQJJLCZNZJCDGZYGCDXMZYSCTLKPHTXHTLBJXJ"
   + "LXSCDQCCBBQJFQZFSLTJBTKQBSXJJLJCHCZDBZJDCZJCCPRNLQCGPFCZLCLCXZDMXMPHGSGZGSZZQJXLWTJPFSYASLCJ"
   + "BTCKWCWMYTCSJJLJCQLWZMALBXYFBPNLSCHTGJWEJJXXGLLJSTGSHJQLZFKCGNNDSZFDEQFHBSAQDGYLBXMMYGSZLDYD"
   + "JMJJRGBJGKGDHGKBLGKBDMBYLXWCXYTTYBKMRJJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ";

  public static short GetGB2312Code(string s)
  {
   if (s == null || s.Length == 0)
    return 32;
   char c = s[0];
   if (c >= ' ' && c <= '~')
   {
    return (short)c;
   }

   byte [] arBytes = Encoding.GetEncoding(936).GetBytes(s);
   if (arBytes.Length < 2)
   {
    return (short)arBytes[0];
   }

   return (short)(arBytes[0] << 8 | arBytes[1]);
  }

  public static short GetGB2312Code(char c)
  {
   return GetGB2312Code("" + (char)c);
  }
 }
}