用VC实现汉字转拼音首字母

参考代码:http://blog.csdn.net/lixiaosan/archive/2006/05/26/756350.aspx
lixiaosan的代码用的是CString在Unicode下有问题,我这里稍作修改,用stl::string和stl::wstring替换CString

代码为两个文件:GenFirstPYLetter.h,GenFirstPYLetter.cpp

 

  1. /* -------------------------------------------------------------------------  
  2. //  FileName    :   GenFirstPYLetter.h  
  3. //  Creator     :   debehe  
  4. //  Date        :   2009/9/28 17:40:24  
  5. //  Brief       :   需要用MultiByte才能正确转换  
  6. //                  Unicode转成MultiByte时不要用W2A  
  7. //  
  8. //  $Id: $  
  9. // -----------------------------------------------------------------------*/   
  10. // How to use:   
  11. //    
  12. // std::wstring strA = L"WPS电子表格";   
  13. // std::wstring strOutput;   
  14. // BOOL bRet = GenFirstPYLetter(strA, strOutput);   
  15. //    
  16. // 结果应该是:   
  17. // strOutput == L"WPSDZBG";   
  18. // bRet == TRUE; // 以字母开头   
  19.   
  20. // -------------------------------------------------------------------------   
  21.   
  22. #ifndef __GETFIRSTLETTER_H__   
  23. #define __GETFIRSTLETTER_H__   
  24.   
  25. #include <Windows.h>   
  26. #include <string>   
  27. // -------------------------------------------------------------------------   
  28.   
  29. /*  
  30. @ Function  : GenFirstPYLetter  
  31. @ brief     : 转换汉字为拼音首字母  
  32. @ parameter : [in]strName  
  33. @ parameter : [out]strResult  
  34. @ return    : 是否以字母开头  
  35. @ remark    :   
  36. @ Date      : 2009/9/28 20:37:19 linyehui  
  37. @*/   
  38. BOOL  GenFirstPYLetter(std::string strName, std::string&strResult );  
  39.   
  40. BOOL  GenFirstPYLetter(std::wstring strName, std::wstring&strResult);  
  41.   
  42.   
  43. // -------------------------------------------------------------------------   
  44. // $Log: $   
  45.   
  46. #endif /* __GETFIRSTLETTER_H__ */   

 

  1. /* -------------------------------------------------------------------------  
  2. //  FileName    :   GenFirstPYLetter.cpp  
  3. //  Creator     :   debehe  
  4. //  Date        :   2009/9/28 17:40:31  
  5. //  Brief       :   需要用MultiByte才能正确转换  
  6. //                  Unicode转成MultiByte时不要用W2A  
  7. //  
  8. //  $Id: $  
  9. // -----------------------------------------------------------------------*/   
  10.   
  11. #include "stdafx.h"   
  12. #include "GenFirstPYLetter.h"   
  13. #include <Windows.h>   
  14.   
  15. // -------------------------------------------------------------------------   
  16.   
  17. /********************************************************************************************  
  18. /* Syntax  
  19. /*        void FirstLetter(int nCode, CString& strLetter)  
  20. /* Remarks:  
  21. /*        Get the first letter of pinyin according to specified Chinese character code.  
  22. /* Parameters:  
  23. /*        nCode         - the code of the chinese character.  
  24. /*        strLetter      - a CString object that is to receive the string of the first letter.  
  25. /* Return Values:  
  26. /*        None.  
  27. /* Author:  
  28. /*        lixiaosan  
  29. /* Create Date:  
  30. /*        05-26-2006  
  31. /********************************************************************************************/   
  32. void  FirstLetter( int  nCode, std::string& strLetter)  
  33. {  
  34.     if (nCode >= 1601 && nCode < 1637) strLetter =  "A" ;  
  35.     if (nCode >= 1637 && nCode < 1833) strLetter =  "B" ;  
  36.     if (nCode >= 1833 && nCode < 2078) strLetter =  "C" ;  
  37.     if (nCode >= 2078 && nCode < 2274) strLetter =  "D" ;  
  38.     if (nCode >= 2274 && nCode < 2302) strLetter =  "E" ;  
  39.     if (nCode >= 2302 && nCode < 2433) strLetter =  "F" ;  
  40.     if (nCode >= 2433 && nCode < 2594) strLetter =  "G" ;  
  41.     if (nCode >= 2594 && nCode < 2787) strLetter =  "H" ;  
  42.     if (nCode >= 2787 && nCode < 3106) strLetter =  "J" ;  
  43.     if (nCode >= 3106 && nCode < 3212) strLetter =  "K" ;  
  44.     if (nCode >= 3212 && nCode < 3472) strLetter =  "L" ;  
  45.     if (nCode >= 3472 && nCode < 3635) strLetter =  "M" ;  
  46.     if (nCode >= 3635 && nCode < 3722) strLetter =  "N" ;  
  47.     if (nCode >= 3722 && nCode < 3730) strLetter =  "O" ;  
  48.     if (nCode >= 3730 && nCode < 3858) strLetter =  "P" ;  
  49.     if (nCode >= 3858 && nCode < 4027) strLetter =  "Q" ;  
  50.     if (nCode >= 4027 && nCode < 4086) strLetter =  "R" ;  
  51.     if (nCode >= 4086 && nCode < 4390) strLetter =  "S" ;  
  52.     if (nCode >= 4390 && nCode < 4558) strLetter =  "T" ;  
  53.     if (nCode >= 4558 && nCode < 4684) strLetter =  "W" ;  
  54.     if (nCode >= 4684 && nCode < 4925) strLetter =  "X" ;  
  55.     if (nCode >= 4925 && nCode < 5249) strLetter =  "Y" ;  
  56.     if (nCode >= 5249 && nCode < 5590) strLetter =  "Z" ;  
  57. }  
  58.   
  59. BOOL  GenFirstPYLetter(std::string strName, std::string&strResult )  
  60. {  
  61.     BOOL  bStartByLetter = FALSE;  
  62.   
  63.     unsigned char  ucHigh, ucLow;  
  64.     int   nCode;  
  65.     std::string strRet;  
  66.   
  67.     strResult.clear();  
  68.   
  69.     if  (strName.empty())  
  70.         return  FALSE;  
  71.   
  72.     if  ((unsigned  char )strName[0] < 0x80 )  
  73.         bStartByLetter = TRUE;  
  74.   
  75.     for  ( size_t  i=0; i < strName.size(); i++)  
  76.     {  
  77.         if  ( (unsigned  char )strName[i] < 0x80 )  
  78.         {  
  79.             strResult += strName[i];  
  80.             continue ;  
  81.         }  
  82.   
  83.         ucHigh = (unsigned char )strName[i];  
  84.         ucLow  = (unsigned char )strName[i+1];  
  85.         if  ( ucHigh < 0xa1 || ucLow < 0xa1)  
  86.             continue ;  
  87.         else   
  88.             // Treat code by section-position as an int type parameter,   
  89.             // so make following change to nCode.   
  90.             nCode = (ucHigh - 0xa0) * 100 + ucLow - 0xa0;  
  91.   
  92.         FirstLetter(nCode, strRet);  
  93.         strResult += strRet;  
  94.         i++;  
  95.     }  
  96.   
  97.     return  bStartByLetter;  
  98. }  
  99.   
  100. BOOL  GenFirstPYLetter(std::wstring strName, std::wstring&strResult)  
  101. {  
  102.     std::string strSource;  
  103.     std::string strOutput;  
  104.       
  105.     // Unicode转换成MultiByte   
  106.     DWORD  dwNum = WideCharToMultiByte(CP_OEMCP,NULL,strName.c_str(),-1,NULL,0,NULL,FALSE);  
  107.     char  *psText;  
  108.     psText = new   char [dwNum];  
  109.     if (!psText)  
  110.     {  
  111.         delete  []psText;  
  112.         return  FALSE;  
  113.     }  
  114.     else   
  115.     {  
  116.         WideCharToMultiByte (CP_OEMCP,NULL,strName.c_str(),-1,psText,dwNum,NULL,FALSE);  
  117.         strSource = psText;  
  118.         delete  []psText;  
  119.     }  
  120.           
  121.     // 转调   
  122.     BOOL  bRet = GenFirstPYLetter(strSource, strOutput);  
  123.   
  124.     // MultiByte转换成Unicode   
  125.     dwNum = MultiByteToWideChar (CP_ACP, 0, strOutput.c_str(), -1, NULL, 0);  
  126.   
  127.     wchar_t  *pwText;  
  128.     pwText = new   wchar_t [dwNum];  
  129.     if (!pwText)  
  130.     {  
  131.         delete  []pwText;  
  132.         return  FALSE;  
  133.     }  
  134.     else   
  135.     {  
  136.         MultiByteToWideChar (CP_ACP, 0, strOutput.c_str(), -1, pwText, dwNum);  
  137.         strResult = pwText;  
  138.         delete  [] pwText;  
  139.     }  
  140.   
  141.     return  bRet;  
  142. }  
  143.   
  144. // -------------------------------------------------------------------------   
  145. // $Log: $  
http://blog.csdn.net/debehe/archive/2009/09/28/4610537.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值