function TForm1.getpyindexchar(hzchar:string):char;
begin
   case word(hzchar[1]) shl 8 +word(hzchar[2]) of
      $B0A1..$B0D0 : RESULT :='a';
      $B0D1..$B2C0 : RESULT :='b';
      $B2C1..$B4F2 : RESULT :='c';
      $B4F3..$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..$D1B8 : RESULT :='x';
      $D1B9..$D4D0 : RESULT :='y';
      $D4D1..$D7F9 : RESULT :='z';
   else
      //result :=char(0);
      result :='v';
   end;
end;function Tform1.getstringpy(sourcestr:string):string;
var
   I_temp : integer;
   B_temp : bool;
begin
   result:='';
   B_temp:=false;
   for i_temp:=1 to length(sourcestr) do
      begin
         if B_temp then
            B_temp:=false
         else
            begin
            if word(sourcestr[i_temp])<176 then
               result:=result+sourcestr[i_temp]
            else
               begin
                  result:=result+getpyindexchar(sourcestr[i_temp]+sourcestr[i_temp+1]);
                  b_temp:=true;
               end;
            end;
      end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
label1.caption:=getstringpy(edit1.text);
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);
              break;
            end;
          end;
          Inc(i);
        end
        else
          Result := Result + AHzStr[i];
        Inc(i);
      end;
    end;
      

  2.   

    如果要Gethzpy的值返回不要中文的,怎么做,或是干脆把不能转换过的直接不要
    eg:  中“华”人.htm  --->   ZFR.htm可以吗?
      

  3.   

    function GetPYCode(AString: string; DealLen: Integer = 16): string;
      function GetHzPy(Hz: string): string;
      var
        C1, Len1, C2: Integer;
        ir: Word;
      begin
        Result := '';
        C1 := 1;
        Len1 := Length(Hz);
        while (C1 <= Len1) do
        begin
          if (ord(Hz[C1]) >= 160) and (ord(Hz[C1 + 1]) >= 160) then
          begin
            ir := (ord(Hz[C1]) - 160) * 100 + ord(Hz[C1 + 1]) - 160;
            C2 := 0;
            while (C2 <= 26) do
            begin
              if (ir >= ChinaCode[C2, 0]) and (ir <= ChinaCode[C2, 1]) then
              begin
                Result := Result + UpCase(chr(C2 + ord('a')));
                break;
              end;
              C2 := C2 + 1;
            end;
          end;
          C1 := C1 + 2;
        end;
      end;var
      sNewFieldName, AHz, APy: string;
      i, j: Integer;
      MbcsByteType: TMbcsByteType;
    begin
      sNewFieldName := '';
      Result := '';
      for i := 1 to Length(AString) do
      begin
        MbcsByteType := ByteType(AString, i);
        if (MbcsByteType <> mbSingleByte) and (Length(sNewFieldName) < DealLen) then
        begin
          if ((MbcsByteType = mbLeadByte) and (Ord(AString[i]) in [176..247])) or
            ((MbcsByteType = mbTrailByte) and (Ord(AString[i - 1]) in [176..247])) then
            sNewFieldName := sNewFieldName + AString[i];
        end
        else if Length(sNewFieldName) = DealLen then
          break;
      end;
      i := 1;
      while i <= Length(sNewFieldName) do
      begin
        AHz := Copy(sNewFieldName, i, 2);
        APy := GetHzPy(AHz);
        Result := Result + APy;
        Inc(i, 2);
      end;
    end;