获得汉字拼音缩写

    技术2022-05-11  78

    方法一:使用说明:您需要的是调用GetPYChar这个函数。例子:       char z= GetPYChar("中");//此时z为'Z'       char a=GetPYChar("爱");// 此时a为'A'

    #include <assert.h>

    //功能:检测Value 是否在Lp..Hp之间//---------------------------------------------------------------------------bool __fastcall In(int Lp, int Hp,int Value){        assert(Lp<=Hp);        return ((Value<=Hp)&&(Value>=Lp));}

    //参数:一个汉字//返回值:该汉字的拼音char __fastcall GetPYChar(AnsiString HZ){assert(HZ.Length()==2);WORD Hi=WORD(HZ[1])<<8;WORD Lo=BYTE(HZ[2]);int n=Hi+Lo;if (In(0xB0A1,0xB0C4,n)) return 'A';if (In(0XB0C5,0XB2C0,n)) return 'B';if (In(0xB2C1,0xB4ED,n)) return 'C';if (In(0xB4EE,0xB6E9,n)) return 'D';if (In(0xB6EA,0xB7A1,n)) return 'E';if (In(0xB7A2,0xB8c0,n)) return 'F';if (In(0xB8C1,0xB9FD,n)) return 'G';if (In(0xB9FE,0xBBF6,n)) return 'H';if (In(0xBBF7,0xBFA5,n)) return 'J';if (In(0xBFA6,0xC0AB,n)) return 'K';if (In(0xC0AC,0xC2E7,n)) return 'L';if (In(0xC2E8,0xC4C2,n)) return 'M';if (In(0xC4C3,0xC5B5,n)) return 'N';if (In(0xC5B6,0xC5BD,n)) return 'O';if (In(0xC5BE,0xC6D9,n)) return 'P';if (In(0xC6DA,0xC8BA,n)) return 'Q';if (In(0xC8BB,0xC8F5,n)) return 'R';if (In(0xC8F6,0xCBF0,n)) return 'S';if (In(0xCBFA,0xCDD9,n)) return 'T';if (In(0xCDDA,0xCEF3,n)) return 'W';if (In(0xCEF4,0xD188,n)) return 'X';if (In(0xD1B9,0xD4D0,n)) return 'Y';if (In(0xD4D1,0xD7F9,n)) return 'Z';return char(0);}

    好了,有了上面的函数,我们就可以将一个汉字字符串了。当然我们得考虑这个汉字字符串中包含有非汉字的情况,这时候我将不对这些非汉字字符进行处理。见下面的函数:

    //函数功能:得一个汉字字符串的拼音//输入参数:HZString---汉字字符串//返回值  :返回汉字字符串对应的拼音字符串//注  意  :#include <ctype.h>AnsiString __fastcall GetPYString(AnsiString HZString){   AnsiString sTemp, sReturn;   AnsiString s =HZString.Trim();   for (int i=1; i<=s.Length(); i++)   {      if (isprint(s[i]))      {         sReturn = sReturn + s[i];      }      else      {         sTemp += s[i];         if (sTemp.Length() == 2)         {            sReturn = sReturn + GetPYChar(sTemp);            sTemp = "";         }      }   }   return sReturn;}//-------------------------------------------------------------------------方法二:对于无法识别的字返回一个下划线"_"String  GetPinyin(String hz){    String pinyin = "";    for(int i=0;i<hz.Length();i+=2){        if(IsDBCSLeadByte(hz.c_str()[i])==0){            pinyin+=hz.c_str()[i];            i--;            continue;        }        int x = ((BYTE)hz.c_str()[i]-160)*100 +(BYTE)hz.c_str()[i+1]-160;        if((x<1601)||(x>5600)) {            pinyin+='_';            continue;        }        char p;        if(x<1637) p='A';        else if(x<1833) p ='B';        else if(x<2078) p = 'C';        else if(x<2274) p = 'D';        else if(x<2302) p = 'E';        else if(x<2433) p = 'F';        else if(x<2594) p = 'G';        else if(x<2787) p = 'H';        else if(x<3106) p = 'J';        else if(x<3212) p = 'K';        else if(x<3472) p = 'L';        else if(x<3635) p = 'M';        else if(x<3722) p = 'N';        else if(x<3730) p = 'O';        else if(x<3858) p = 'P';        else if(x<4027) p = 'Q';        else if(x<4086) p = 'R';        else if(x<4390) p = 'S';        else if(x<4558) p = 'T';        else if(x<4684) p = 'W';        else if(x<4925) p = 'X';        else if(x<5249) p = 'Y';        else if(x<5590) p = 'Z';        pinyin += p;    }    return pinyin;}

     


    最新回复(0)