汉字转拼音缩写的BCB代码
这段代码是我从VC转换过来的,但是有问题,希望各位帮我看看修正一下:
代码如下:
void __fastcall TForm1::Button2Click(TObject *Sender)
{//转换
String CHChar,SpellChar;
CHChar = Edit1->Text;//获取汉字
GetFirstLetter(CHChar, SpellChar);//调用汉字转拼音缩写程序
Edit2->Text = SpellChar;//设置拼音缩写到文本框
}
//---------------------------------------
//通过汉字获得汉字拼音首字母串
void TForm1::GetFirstLetter(String strName, String& strFirstLetter)
{
//存放一个汉字高低位字节
TBYTE ucHigh, ucLow;
//存放汉字区位码
int nCode;
//存放汉字拼音首字母
String strRet;
//复位首字母
// strFirstLetter.IsEmpty();
//单个字节遍历
char *p=strName.c_str();
for (int i=0; i<strName.Length(); i++)
{
//判断是否是组成汉字的字节
if ( (TBYTE)*p < 0x80 )
{
p++;
continue;
}
//获取汉字高位字节
ucHigh = (TBYTE)*p;
//获取汉字低位字节
ucLow = (TBYTE)*p++;
//汉字的内码是从0xa1开始编码
if ( ucHigh < 0xa1 || ucLow < 0xa1)
{
p++;
continue;
}
else
{
//计算汉字区位码
nCode = (ucHigh - 0xa0) * 100 + ucLow - 0xa0;
//通过汉字区位码获得汉字拼音首字母
FirstLetter(nCode, strRet);
//将首字母拼接成串
strFirstLetter += strRet;
p++;
}
}
}
//---------------------------------------
void TForm1::FirstLetter(int nCode, String& strLetter)
{
if(nCode >= 1601 && nCode < 1637) strLetter = "a";
if(nCode >= 1637 && nCode < 1833) strLetter = "b";
if(nCode >= 1833 && nCode < 2078) strLetter = "c";
if(nCode >= 2078 && nCode < 2274) strLetter = "d";
if(nCode >= 2274 && nCode < 2302) strLetter = "e";
if(nCode >= 2302 && nCode < 2433) strLetter = "f";
if(nCode >= 2433 && nCode < 2594) strLetter = "g";
if(nCode >= 2594 && nCode < 2787) strLetter = "h";
if(nCode >= 2787 && nCode < 3106) strLetter = "j";
if(nCode >= 3106 && nCode < 3212) strLetter = "k";
if(nCode >= 3212 && nCode < 3472) strLetter = "l";
if(nCode >= 3472 && nCode < 3635) strLetter = "m";
if(nCode >= 3635 && nCode < 3722) strLetter = "n";
if(nCode >= 3722 && nCode < 3730) strLetter = "o";
if(nCode >= 3730 && nCode < 3858) strLetter = "p";
if(nCode >= 3858 && nCode < 4027) strLetter = "q";
if(nCode >= 4027 && nCode < 4086) strLetter = "r";
if(nCode >= 4086 && nCode < 4390) strLetter = "s";
if(nCode >= 4390 && nCode < 4558) strLetter = "t";
if(nCode >= 4558 && nCode < 4684) strLetter = "w";
if(nCode >= 4684 && nCode < 4925) strLetter = "x";
if(nCode >= 4925 && nCode < 5249) strLetter = "y";
if(nCode >= 5249 && nCode < 5590) strLetter = "z";
}
//-------------------------------------------
[解决办法]
我也用过这个方法,但是有部分汉字不行,后来我用下面这个方法
AnsiString ConvertHZToPYp(char *as_HzString)
{
static int li_SecPosValue[]={1601,1637,1833,2078,2274,2302,2433,2594,2787,3106,3212,3472,3635,3722,3730,3858,4027,4086,4390,4558,4684,4925,5249};
static char* lc_FirstLetter[] = {"A", "B","C","D","E","F","G","H","J","K","L","M","N","O","P","Q","R","S","T","W","X","Y","Z"};
static char* ls_SecondSecTable =
"CJWGNSPGCGNE[Y[BTYYZDXYKYGT[JNNJQMBSGZSCYJSYY[PGKBZGY[YWJKGKLJYWKPJQHY[W[DZLSGMRYPYWWCCKZNKYYGTTNJJNYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGKJFYXJWZLTBNCXJJJJTXDTTSQZYCDXXHGCK[PHFFSS[YBGXLPPBYLL[HLXS[ZM[JHSOJNGHDZQYKLGJHSGQZHXQGKEZZWYSCSCJXYEYXADZPMDSSMZJZQJYZC[J[WQJBYZPXGZNZCPWHKXHQKMWFBPBYDTJZZKQHY"
"LYGXFPTYJYYZPSZLFCHMQSHGMXXSXJ[[DCSBBQBEFSJYHXWGZKPYLQBGLDLCCTNMAYDDKSSNGYCSGXLYZAYBNPTSDKDYLHGYMYLCXPY[JNDQJWXQXFYYFJLEJPZRXCCQWQQSBNKYMGPLB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YCHGHLYRZQLCYQJNZSQTKQJYMSZSWLCFQQQXYFGGYPTQWLMCRNFKKFSYYLQBMQAMMMYXCTPSHCPTXXZZSMPHPSHMCLMLDQFYQXSZYYDYJZZHQPDSZGLSTJBCKBXYQZJSGPSXQZQZRQTBDKYXZK"
"HHGFLBCSMDLDGDZDBLZYYCXNNCSYBZBFGLZZXSWMSCCMQNJQSBDQSJTXXMBLTXZCLZSHZCXRQJGJYLXZFJPHYMZQQYDFQJJLZZNZJCDGZYGCTXMZYSCTLKPHTXHTLBJXJLXSCDQXCBBTJFQZFSLTJBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPHJSGZGSZZQLYLWTJPFSYASMCJBTZKYCWMYTCSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNNSZFDEQ"
"FHBSAQTGYLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ";
AnsiString result = "";
int H,L,W;
unsigned stringlen = strlen(as_HzString);
for(int i = 0; i < stringlen; i ++ )
{
H = (unsigned char)(as_HzString[i]);
L = (unsigned char)(as_HzString[i+1]);
if(H < 0xA1
[解决办法]
L < 0xA1)
{
result += as_HzString[i];
continue;
}
else W = (H - 160) * 100 + L - 160;
if(W > 1600 && W < 5590)
{
for(int j = 22; j >= 0; j --)
if(W >= li_SecPosValue[j])
{
result += lc_FirstLetter[j];
i++;
break;
}
continue;
}
else
{
i++;
W = ( H - 160 - 56 )*94 + L - 161;
if(W >= 0 && W <= 3007)
result += ls_SecondSecTable[W];
else
{
result += (char)H;
result += (char)L;
}
}
}
return result;
}