8.VC(custom)-搜索汉字返回拼音首字母

工具:vs2005

环境:unicode

 DBCS ;double-byte character set,双字节字符集,用于对 Chinese, Japanese and Korean语言支持

IsDBCSLeadByte( BYTE ); 如果该字节是DBCS字符的第一个字节,则返回非0值

由于原来的搜索匹配占的CPU过高,所以上网查了下资料,换了个简单的搜索,少部分汉字可能无返回.记录如下:

bool In(wchar_t start, wchar_t end, wchar_t code)
{
	if(code >= start&&code<=end)
	{
		return true;
	}
	return false;
}
wchar_t convert(wchar_t n)//返回首字母的函数
{
	if   (In(0xB0A1,0xB0C4,n))   return   _T('a');
	if   (In(0XB0C5,0XB2C0,n))   return   _T('b');
	if   (In(0xB2C1,0xB4ED,n))   return   _T('c');
	if   (In(0xB4EE,0xB6E9,n))   return   _T('d');
	if   (In(0xB6EA,0xB7A1,n))   return   _T('e');
	if   (In(0xB7A2,0xB8c0,n))   return   _T('f');
	if   (In(0xB8C1,0xB9FD,n))   return   _T('g');
	if   (In(0xB9FE,0xBBF6,n))   return   _T('h');
	if   (In(0xBBF7,0xBFA5,n))   return   _T('j');
	if   (In(0xBFA6,0xC0AB,n))   return   _T('k');
	if   (In(0xC0AC,0xC2E7,n))   return   _T('l');
	if   (In(0xC2E8,0xC4C2,n))   return   _T('m');
	if   (In(0xC4C3,0xC5B5,n))   return   _T('n');
	if   (In(0xC5B6,0xC5BD,n))   return   _T('o');
	if   (In(0xC5BE,0xC6D9,n))   return   _T('p');
	if   (In(0xC6DA,0xC8BA,n))   return   _T('q');
	if   (In(0xC8BB,0xC8F5,n))   return   _T('r');
	if   (In(0xC8F6,0xCBF0,n))   return   _T('s');
	if   (In(0xCBFA,0xCDD9,n))   return   _T('t');
	if   (In(0xCDDA,0xCEF3,n))   return   _T('w');
	if   (In(0xCEF4,0xD188,n))   return   _T('x');
	if   (In(0xD1B9,0xD4D0,n))   return   _T('y');
	if   (In(0xD4D1,0xD7F9,n))   return   _T('z');
	return   n;
}

CString GetSouPY(CString str)/*输入一串字符,输出:汉字将转换成首字母代替,其佘字符不变,如:输入:12a黄金时代,输出:12ahjsd,*/
{
#ifdef UNICODE
	DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, str.GetBuffer(0), -1, NULL, 0, NULL, FALSE);
	char *psText;
	psText = new char[dwNum];
	if (!psText)
		delete []psText;
	WideCharToMultiByte(CP_OEMCP, NULL, str.GetBuffer(0), -1, psText, dwNum, NULL, FALSE);
#endif
	std::string   sChinese=psText;
	char c;
	char chr[3];
	wchar_t   wchr   =   0;
	int len=sChinese.length()+1;
	CString szInfo ;
	for   (int i=0,j=0; j<(sChinese.length());   ++i)
	{
		memset(chr,0x00,sizeof(chr));
		do
		{
          chr[0]   =   sChinese[j++];
		  if (!IsDBCSLeadByte(chr[0]))
		  {
           szInfo += chr[0];
		   if (j==sChinese.length())
		   {
			   break;
		   }
		  }
		}while(!IsDBCSLeadByte(chr[0]));
		if (j<sChinese.length())
		{
			chr[1] = sChinese[j++];
			chr[2] = '/0';
			wchr = 0;
			wchr = (chr[0] &0xff)<<8;
			wchr |= (chr[1] &0xff);
			if(convert(wchr)!=wchr)
				szInfo += convert(wchr);
		}
		else
			break ;
	}
	CString PinYin=szInfo;
	return  PinYin;
}


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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