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

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

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

<textarea cols="50" rows="15" name="code" class="cpp">/* ------------------------------------------------------------------------- // FileName : GenFirstPYLetter.h // Creator : debehe // Date : 2009/9/28 17:40:24 // Brief : 需要用MultiByte才能正确转换 // Unicode转成MultiByte时不要用W2A // // $Id: $ // -----------------------------------------------------------------------*/ // How to use: // // std::wstring strA = L"WPS电子表格"; // std::wstring strOutput; // BOOL bRet = GenFirstPYLetter(strA, strOutput); // // 结果应该是: // strOutput == L"WPSDZBG"; // bRet == TRUE; // 以字母开头 // ------------------------------------------------------------------------- #ifndef __GETFIRSTLETTER_H__ #define __GETFIRSTLETTER_H__ #include &lt;Windows.h&gt; #include &lt;string&gt; // ------------------------------------------------------------------------- /* @ Function : GenFirstPYLetter @ brief : 转换汉字为拼音首字母 @ parameter : [in]strName @ parameter : [out]strResult @ return : 是否以字母开头 @ remark : @ Date : 2009/9/28 20:37:19 linyehui @*/ BOOL GenFirstPYLetter(std::string strName, std::string&amp;strResult ); BOOL GenFirstPYLetter(std::wstring strName, std::wstring&amp;strResult); // ------------------------------------------------------------------------- // $Log: $ #endif /* __GETFIRSTLETTER_H__ */ </textarea>

<textarea cols="50" rows="15" name="code" class="cpp">/* ------------------------------------------------------------------------- // FileName : GenFirstPYLetter.cpp // Creator : debehe // Date : 2009/9/28 17:40:31 // Brief : 需要用MultiByte才能正确转换 // Unicode转成MultiByte时不要用W2A // // $Id: $ // -----------------------------------------------------------------------*/ #include "stdafx.h" #include "GenFirstPYLetter.h" #include &lt;Windows.h&gt; // ------------------------------------------------------------------------- /******************************************************************************************** /* Syntax /* void FirstLetter(int nCode, CString&amp; strLetter) /* Remarks: /* Get the first letter of pinyin according to specified Chinese character code. /* Parameters: /* nCode - the code of the chinese character. /* strLetter - a CString object that is to receive the string of the first letter. /* Return Values: /* None. /* Author: /* lixiaosan /* Create Date: /* 05-26-2006 /********************************************************************************************/ void FirstLetter(int nCode, std::string&amp; strLetter) { if(nCode &gt;= 1601 &amp;&amp; nCode &lt; 1637) strLetter = "A"; if(nCode &gt;= 1637 &amp;&amp; nCode &lt; 1833) strLetter = "B"; if(nCode &gt;= 1833 &amp;&amp; nCode &lt; 2078) strLetter = "C"; if(nCode &gt;= 2078 &amp;&amp; nCode &lt; 2274) strLetter = "D"; if(nCode &gt;= 2274 &amp;&amp; nCode &lt; 2302) strLetter = "E"; if(nCode &gt;= 2302 &amp;&amp; nCode &lt; 2433) strLetter = "F"; if(nCode &gt;= 2433 &amp;&amp; nCode &lt; 2594) strLetter = "G"; if(nCode &gt;= 2594 &amp;&amp; nCode &lt; 2787) strLetter = "H"; if(nCode &gt;= 2787 &amp;&amp; nCode &lt; 3106) strLetter = "J"; if(nCode &gt;= 3106 &amp;&amp; nCode &lt; 3212) strLetter = "K"; if(nCode &gt;= 3212 &amp;&amp; nCode &lt; 3472) strLetter = "L"; if(nCode &gt;= 3472 &amp;&amp; nCode &lt; 3635) strLetter = "M"; if(nCode &gt;= 3635 &amp;&amp; nCode &lt; 3722) strLetter = "N"; if(nCode &gt;= 3722 &amp;&amp; nCode &lt; 3730) strLetter = "O"; if(nCode &gt;= 3730 &amp;&amp; nCode &lt; 3858) strLetter = "P"; if(nCode &gt;= 3858 &amp;&amp; nCode &lt; 4027) strLetter = "Q"; if(nCode &gt;= 4027 &amp;&amp; nCode &lt; 4086) strLetter = "R"; if(nCode &gt;= 4086 &amp;&amp; nCode &lt; 4390) strLetter = "S"; if(nCode &gt;= 4390 &amp;&amp; nCode &lt; 4558) strLetter = "T"; if(nCode &gt;= 4558 &amp;&amp; nCode &lt; 4684) strLetter = "W"; if(nCode &gt;= 4684 &amp;&amp; nCode &lt; 4925) strLetter = "X"; if(nCode &gt;= 4925 &amp;&amp; nCode &lt; 5249) strLetter = "Y"; if(nCode &gt;= 5249 &amp;&amp; nCode &lt; 5590) strLetter = "Z"; } BOOL GenFirstPYLetter(std::string strName, std::string&amp;strResult ) { BOOL bStartByLetter = FALSE; unsigned char ucHigh, ucLow; int nCode; std::string strRet; strResult.clear(); if (strName.empty()) return FALSE; if ((unsigned char)strName[0] &lt; 0x80 ) bStartByLetter = TRUE; for (size_t i=0; i &lt; strName.size(); i++) { if ( (unsigned char)strName[i] &lt; 0x80 ) { strResult += strName[i]; continue; } ucHigh = (unsigned char)strName[i]; ucLow = (unsigned char)strName[i+1]; if ( ucHigh &lt; 0xa1 || ucLow &lt; 0xa1) continue; else // Treat code by section-position as an int type parameter, // so make following change to nCode. nCode = (ucHigh - 0xa0) * 100 + ucLow - 0xa0; FirstLetter(nCode, strRet); strResult += strRet; i++; } return bStartByLetter; } BOOL GenFirstPYLetter(std::wstring strName, std::wstring&amp;strResult) { std::string strSource; std::string strOutput; // Unicode转换成MultiByte DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,strName.c_str(),-1,NULL,0,NULL,FALSE); char *psText; psText = new char[dwNum]; if(!psText) { delete []psText; return FALSE; } else { WideCharToMultiByte (CP_OEMCP,NULL,strName.c_str(),-1,psText,dwNum,NULL,FALSE); strSource = psText; delete []psText; } // 转调 BOOL bRet = GenFirstPYLetter(strSource, strOutput); // MultiByte转换成Unicode dwNum = MultiByteToWideChar (CP_ACP, 0, strOutput.c_str(), -1, NULL, 0); wchar_t *pwText; pwText = new wchar_t[dwNum]; if(!pwText) { delete []pwText; return FALSE; } else { MultiByteToWideChar (CP_ACP, 0, strOutput.c_str(), -1, pwText, dwNum); strResult = pwText; delete [] pwText; } return bRet; } // ------------------------------------------------------------------------- // $Log: $ </textarea>

你可能感兴趣的:(Date,String,null,delete,Parameters,character)