经常看见这样一段获取汉字拼音首字母的程序
function GetPYIndexChar(strChinese: string;bUpCase: Boolean = True): char;begin// 根据汉字表中拼音首字符分别为“A”至“Z”的汉字内码范围,// 要检索的汉字只需要检查它的内码位于哪一个首字符的范围内,// 就可以判断出它的拼音首字符。case WORD(strChinese[1]) shl 8 + WORD(strChinese[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';elseresult := char(0);end;if not bUpCase thenbegin // 转换为小写result := Chr(Ord(result)+32);end;end;////////////////////////////////////////////////////////////////////////////// 函数: GetPYIndexStr(strChinese: string;bUpCase: Boolean = True): string;//// 函数功能:获取多个汉字的拼音首字符组成的字符串.// 例: GetPYIndexStr('程') 将返回'C'.// GetPYIndexStr('程序')将返回'CX'.//// 第二个参数决定返回大写还是小写 , 缺省为大写 .////////////////////////////////////////////////////////////////////////////function GetPYIndexStr(strChinese: string;bUpCase: Boolean = True): string;varstrChineseTemp : string;cTemp : Char;beginresult := '';strChineseTemp := strChinese;while strChineseTemp<>'' dobegincTemp := GetPYIndexChar(strChineseTemp);if not bUpCase thenbegin // 转换为小写cTemp := Chr(Ord(cTemp)+32);end;result := result + string(cTemp);strChineseTemp := Copy(strChineseTemp,3,Length(strChineseTemp));end;end;end.
这个程序在很多书上都有,但是我在用这段程序的时候发现了一个问题,我在转换一首歌“我还能做什么”时,发现演唱者小柯,黄绮姗中的“绮”的第一个字节是$E7,“姗”的第一个字节是$E6,大家可以看看程序中字母"Q"对应的内码范围,这两个字根本无法标注拼音,导致这段程序我无法使用,另一个流传较广的版本我还没有试验,可能不会出问题,该版本如下
看看获取汉字拼音组件 v4.1
相关代码:
function GetHzPy(HzChar: PChar; Len: Integer): String;
var
  C: Char;
  Index: Integer;
begin
  Result := '';
  if (Len > 1) and (HzChar[0] >= #129) and (HzChar[1] >= #64) then
  begin
    //是否为 GBK 字符
    case HzChar[0] of
      #163:  // 全角 ASCII
      begin
        C := Chr(Ord(HzChar[1]) - 128);
        if C in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']'] then
          Result := C
        else
          Result := '';
      end;
      #162: // 罗马数字
      begin
        if HzChar[1] > #160 then
          Result := CharIndex[Ord(HzChar[1]) - 160]
        else
          Result := '';
      end;
      #166: // 希腊字母
      begin
        if HzChar[1] in [#$A1..#$B8] then
          Result := CharIndex2[Ord(HzChar[1]) - $A0]
        else if HzChar[1] in [#$C1..#$D8] then
          Result := CharIndex2[Ord(HzChar[1]) - $C0]
        else
          Result := '';
      end;
      else
      begin  // 获得拼音索引
        Index := PyCodeIndex[Ord(HzChar[0]) - 128, Ord(HzChar[1]) - 63];
        if Index = 0 then
          Result := ''
        else
          Result := PyMusicCode[Index];
      end;
    end;
  end
  else if Len > 0 then
  begin
    //在 GBK 字符集外, 即半角字符
    if HzChar[0] in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']',
      '.', '!', '@', '#', '$', '%', '^', '&', '*', '-', '+',
      '<', '>', '?', ':', '"'] then
      Result := HzChar[0]
    else
      Result := '';
  end;
end;