function GetPyChar( const HZ: AnsiString): string ; const HZCode: array [ 0 .. 25 , 0 .. 1 ] of Integer = (( 1601 , 1636 ), ( 1637 , 1832 ), ( 1833 , 2077 ), ( 2078 , 2273 ), ( 2274 , 2301 ), ( 2302 , 2432 ), ( 2433 , 2593 ), ( 2594 , 2786 ), ( 9999 , 0000 ), ( 2787 , 3105 ), ( 3106 , 3211 ), ( 3212 , 3471 ), ( 3472 , 3634 ), ( 3635 , 3722 ), ( 3723 , 3729 ), ( 3730 , 3857 ), ( 3858 , 4026 ), ( 4027 , 4085 ), ( 4086 , 4389 ), ( 4390 , 4557 ), ( 9999 , 0000 ), ( 9999 , 0000 ), ( 4558 , 4683 ), ( 4684 , 4924 ), ( 4925 , 5248 ), ( 5249 , 5589 )); var i,j,HzOrd: Integer; begin i := 1 ; while i <= Length(HZ) do begin if (HZ[i] >= #160 ) and (HZ[i + 1 ]>= #160 ) then begin HzOrd := (Ord(HZ[i]) - 160 ) * 100 + Ord(HZ[i + 1 ]) - 160 ; for j := 0 to 25 do begin if (HzOrd >= HZCode[j][ 0 ]) and (HzOrd <= HZCode[j][ 1 ]) then begin Result := Result + Char(Byte( 'A' ) + j); Break; end ; end ; Inc(i); end else Result := Result + HZ[i]; Inc(i); end ; end ; //测试: procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(GetPyChar( '万一的 Delphi 博客' )); //结果:WYD Delphi BK end ; //只单独汉字: function GetPyFitstChar(HZ: AnsiString): Char; begin case LoWord(HZ[ 1 ]) shl 8 + LoWord(HZ[ 2 ]) of $B0A1 .. $B0C4 : Result := 'A' ; $B0C5 .. $B2C0 : Result := 'B' ; $B2C1 .. $B4ED : Result := 'C' ; $B4EE .. $B6E9 : Result := 'D' ; $B6EA .. $B7A1 : Result := 'E' ; $B7A2 .. $B8C0 : Result := 'F' ; $B8C1 .. $B9FD : Result := 'G' ; $B9FE .. $BBF6 : Result := 'H' ; $BBF7 .. $BFA5 : Result := 'J' ; $BFA6 .. $C0AB : Result := 'K' ; $C0AC .. $C2E7 : Result := 'L' ; $C2E8 .. $C4C2 : Result := 'M' ; $C4C3 .. $C5B5 : Result := 'N' ; $C5B6 .. $C5BD : Result := 'O' ; $C5BE .. $C6D9 : Result := 'P' ; $C6DA .. $C8BA : Result := 'Q' ; $C8BB .. $C8F5 : Result := 'R' ; $C8F6 .. $CBF9 : Result := 'S' ; $CBFA .. $CDD9 : Result := 'T' ; $CDDA .. $CEF3 : Result := 'W' ; $CEF4 .. $D188 : Result := 'X' ; $D1B9 .. $D4D0 : Result := 'Y' ; $D4D1 .. $D7F9 : Result := 'Z' ; else Result := Char( 0 ); end ; end ; //测试: procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(GetPyFitstChar( '万一的 Delphi 博客' )); //结果:W end ; 转载自http://www.cnblogs.com/del/archive/2008/10/27/968825.html
// 获取指定汉字的拼音索引字母,如:“汉”的索引字母是“H” function GetPYIndexChar( hzchar:string):char; begin case WORD(hzchar[1]) shl 8 + WORD(hzchar[2]) of $B0A1..$B0C4 : result := 'A'; $B0C5..$B2C0 : result := 'B'; $B2C1..$B4ED : result := 'C'; $B4EE..$B6E9 : result := 'D'; $B6EA..$B7A1 : result := 'E'; $B7A2..$B8C0 : result := 'F'; $B8C1..$B9FD : result := 'G'; $B9FE..$BBF6 : result := 'H'; $BBF7..$BFA5 : result := 'J'; $BFA6..$C0AB : result := 'K'; $C0AC..$C2E7 : result := 'L'; $C2E8..$C4C2 : result := 'M'; $C4C3..$C5B5 : result := 'N'; $C5B6..$C5BD : result := 'O'; $C5BE..$C6D9 : result := 'P'; $C6DA..$C8BA : result := 'Q'; $C8BB..$C8F5 : result := 'R'; $C8F6..$CBF9 : result := 'S'; $CBFA..$CDD9 : result := 'T'; $CDDA..$CEF3 : result := 'W'; $CEF4..$D188 : result := 'X'; $D1B9..$D4D0 : result := 'Y'; $D4D1..$D7F9 : result := 'Z'; else result := char(32); end; end; //------------------------------------------------------------------------------------------------ //加上两个Edit控件(ChineseEdt、PYEdt),和一个Button(btnConvert) //按钮按下时将ChineseEdt.Text中的汉字首字母保存有PYEdt.Text中 //------------------------------------------------------------------------------------------------ procedure TMainForm.btnConvertClick(Sender: TObject); var I: Integer; PY: string; s: string; begin s := '' ; for I := 1 to Length(ChineseEdt.Text) div 2 do begin PY := Copy(ChineseEdt.Text, I * 2 - 1, 2); s := s + GetPYIndexChar(PY); end; PYEdt.Text := s; end;
//获取多个汉字的第一个拼音缩写(中国-ZG) function Tform1.GetPyChar(const HZ: AnsiString): string; const HZCode: array[0..25, 0..1] of Integer = ((1601, 1636), (1637, 1832), (1833, 2077), (2078, 2273), (2274, 2301), (2302, 2432), (2433, 2593), (2594, 2786), (9999, 0000), (2787, 3105), (3106, 3211), (3212, 3471), (3472, 3634), (3635, 3722), (3723, 3729), (3730, 3857), (3858, 4026), (4027, 4085), (4086, 4389), (4390, 4557), (9999, 0000), (9999, 0000), (4558, 4683), (4684, 4924), (4925, 5248), (5249, 5589)); var i,j,HzOrd: Integer; begin i := 1; while i <= Length(HZ) do begin if (HZ[i] >= #160) and (HZ[i + 1] >= #160) then begin HzOrd := (Ord(HZ[i]) - 160) * 100 + Ord(HZ[i + 1]) - 160; for j := 0 to 25 do begin if (HzOrd >= HZCode[j][0]) and (HzOrd <= HZCode[j][1]) then begin Result := Result + Char(Byte('A') + j); Break; end; end; Inc(i); end else Result := Result + HZ[i]; Inc(i); end; end;procedure TForm1.Button2Click(Sender: TObject); begin ShowMessage(GetPyChar(ChineseEdit.Text)); end;
转载自http://www.cnblogs.com/del/archive/2008/10/27/968825.html
function GetPYIndexChar( hzchar:string):char;
begin
case WORD(hzchar[1]) shl 8 + WORD(hzchar[2]) of
$B0A1..$B0C4 : result := 'A';
$B0C5..$B2C0 : result := 'B';
$B2C1..$B4ED : result := 'C';
$B4EE..$B6E9 : result := 'D';
$B6EA..$B7A1 : result := 'E';
$B7A2..$B8C0 : result := 'F';
$B8C1..$B9FD : result := 'G';
$B9FE..$BBF6 : result := 'H';
$BBF7..$BFA5 : result := 'J';
$BFA6..$C0AB : result := 'K';
$C0AC..$C2E7 : result := 'L';
$C2E8..$C4C2 : result := 'M';
$C4C3..$C5B5 : result := 'N';
$C5B6..$C5BD : result := 'O';
$C5BE..$C6D9 : result := 'P';
$C6DA..$C8BA : result := 'Q';
$C8BB..$C8F5 : result := 'R';
$C8F6..$CBF9 : result := 'S';
$CBFA..$CDD9 : result := 'T';
$CDDA..$CEF3 : result := 'W';
$CEF4..$D188 : result := 'X';
$D1B9..$D4D0 : result := 'Y';
$D4D1..$D7F9 : result := 'Z';
else
result := char(32);
end;
end;
//------------------------------------------------------------------------------------------------
//加上两个Edit控件(ChineseEdt、PYEdt),和一个Button(btnConvert)
//按钮按下时将ChineseEdt.Text中的汉字首字母保存有PYEdt.Text中
//------------------------------------------------------------------------------------------------
procedure TMainForm.btnConvertClick(Sender: TObject);
var
I: Integer;
PY: string;
s: string;
begin
s := '' ;
for I := 1 to Length(ChineseEdt.Text) div 2 do
begin
PY := Copy(ChineseEdt.Text, I * 2 - 1, 2);
s := s + GetPYIndexChar(PY);
end;
PYEdt.Text := s;
end;
function Tform1.GetPyChar(const HZ: AnsiString): string;
const
HZCode: array[0..25, 0..1] of Integer = ((1601, 1636), (1637, 1832), (1833, 2077),
(2078, 2273), (2274, 2301), (2302, 2432), (2433, 2593), (2594, 2786), (9999, 0000),
(2787, 3105), (3106, 3211), (3212, 3471), (3472, 3634), (3635, 3722), (3723, 3729),
(3730, 3857), (3858, 4026), (4027, 4085), (4086, 4389), (4390, 4557), (9999, 0000),
(9999, 0000), (4558, 4683), (4684, 4924), (4925, 5248), (5249, 5589));
var
i,j,HzOrd: Integer;
begin
i := 1;
while i <= Length(HZ) do
begin
if (HZ[i] >= #160) and (HZ[i + 1] >= #160) then
begin
HzOrd := (Ord(HZ[i]) - 160) * 100 + Ord(HZ[i + 1]) - 160;
for j := 0 to 25 do
begin
if (HzOrd >= HZCode[j][0]) and (HzOrd <= HZCode[j][1]) then
begin
Result := Result + Char(Byte('A') + j);
Break;
end;
end;
Inc(i);
end else Result := Result + HZ[i];
Inc(i);
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage(GetPyChar(ChineseEdit.Text));
end;