这是一个取汉字拼音首字母的函数,但不知为什么一碰到"陈登华文选集"这几个汉字就死循环,帮我看看错误出在那里,谢谢!下面是这个函数!function ChineseToSpell(Chinese: string): string;
  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;
var
  i : Integer;
  C : Char;
  Spell: string;
begin
  Spell := '';
  i := 1;
  while i <= Length(Chinese) do
  begin
    if Chinese[i] <= Chr(127) then
    begin
      Spell := Spell + UpCase(Chinese[i]);
      i := i + 1;
    end
    else
    begin
      C := GetPYIndexChar(Copy(Chinese, i, 2));
      if C <> Char(32) then
      begin
        Spell := Spell + UpCase(C);
        i := i + 2;
      end;
    end;
  end;
  Result := Spell;
end;
这是调用这个函数:
procedure TForm1.Button1Click(Sender: TObject);
var
  Spell : string;
begin
  Spell := ChineseToSpell('陈登华文选集');
end;

解决方案 »

  1.   

    function GetHzPy(const AHzStr: string): string;
    const
      ChinaCode: 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(AHzStr) do
      begin
        if (AHzStr[i] >= #160) and (AHzStr[i + 1] >= #160) then
        begin
          HzOrd := (Ord(AHzStr[i]) - 160) * 100 + Ord(AHzStr[i + 1]) - 160;
          for j := 0 to 25 do
          begin
            if (HzOrd >= ChinaCode[j][0]) and (HzOrd <= ChinaCode[j][1]) then
            begin
              Result := Result + char(byte('A') + j);  //想要小写此处改成'a'
              break;
            end;
          end;
          Inc(i);
        end
        else
          Result := Result + AHzStr[i];
      Inc(i);
      end;
    end;
      

  2.   

    经过测试与改进,你的错误是:
          C := GetPYIndexChar(Copy(Chinese, i, 2));
          if C <> Char(32) then {在这里;若不成立,下面的i := i + 2执行不到,成死循环}
          begin
            Spell := Spell + UpCase(C);
            i := i + 2;
          end;
    要改成
          C := GetPYIndexChar(Copy(Chinese, i, 2));
          if C <> Char(32) then
            Spell := Spell + UpCase(C);
          i := i + 2;