/// <summary>
/// 完整转换函数,输出全拼的结果
/// </summary>
/// <param name="CnString">传入的中文字符串</param>
/// <returns>转换出的拼音组合</returns>
public static string FullConvert(string CnString)
{
    byte[] btArray = new byte[2];
    int cAscii = 0;
    short idx1, idx2;
    StringBuilder sbResult = new StringBuilder();
    char[] tempCArray = CnString.ToCharArray();

    for (int idx = 0; idx < tempCArray.Length; idx++)
    {
        btArray = Encoding.Default.GetBytes(tempCArray[idx].ToString());
        if (btArray.Length == 1)
            sbResult.Append(tempCArray[idx]);
        else
        {
            idx1 = (short)btArray[0];
            idx2 = (short)btArray[1];
            cAscii = idx1 * 256 + idx2 - 65536;
            if (cAscii > 0  &&  cAscii < 160)
                sbResult.Append(tempCArray[idx]);
            else
            {
                for (int i = pyValueArr.Length - 1; i >= 0; i--)
                {
                    if (pyValueArr[i] <= cAscii)
                    {
                        sbResult.Append(pyCharacterArr[i]);
                        break;
                    }
                }
            }
        }
    }
    return sbResult.ToString();
}

/// <summary>
/// 只输出首字母的组合
/// </summary>
/// <param name="CnString">待转换的中文字符串</param>
/// <returns>拼音首字母组合结果</returns>
public static string CapitalCovert(string CnString)
{
    StringBuilder sbTemp = new StringBuilder();
    for (int i = 0; i < CnString.Length; i++)
    {
        sbTemp.Append(GetCnCharAreaCode(CnString.Substring(i, 1)));
    }

    return sbTemp.ToString();
}

使用也非常简单:


Console.WriteLine("输出汉字的全拼:");
Console.WriteLine(DeltaCat.Net.Cn2Py.FullConvert("三角猫"));

Console.WriteLine("输出汉字的首字母组合:");
Console.WriteLine(DeltaCat.Net.Cn2Py.CapitalCovert("三角猫 @ 真U14.CN"));

Chinese converted into pinyin_i++