Public class converttopinying
{
/// <Summary>
/// Abbreviated Chinese character to PinYin
/// </Summary>
/// <Param name = "str"> string of Chinese characters to be converted </param>
/// <Returns> pinyin abbreviation </returns>
Public static string getpystring (string Str)
{
String tempstr = "";
If (! Ischina (STR ))
{
Return STR;
}
Foreach (char C in Str)
{
If (INT) C >=33 & (INT) C <= 126)
{// Reserved letters and symbols
Tempstr + = C. tostring ();
}
Else
{// Accumulate the pinyin initials
Tempstr + = getpychar (C. tostring ());
}
}
Return tempstr;
}
Public static bool ischina (string cstring)
{
Bool boolvalue = false;
For (INT I = 0; I <cstring. length; I ++)
{
If (convert. toint32 (convert. tochar (cstring. substring (I, 1) <convert. toint32 (convert. tochar (128 )))
{
Boolvalue = false;
}
Else
{
Return boolvalue = true;
}
}
Return boolvalue;
}
/// <Summary>
/// Obtain the pinyin initials of a single character
/// </Summary>
/// <Param name = "C"> single Chinese character to be converted </param>
/// <Returns> pinyin initials </returns>
Private Static string getpychar (string C)
{
Byte [] array = new byte [2];
Array = system. Text. encoding. Default. getbytes (C );
Int I = (short) (array [0]-'\ 0') * 256 + (short) (array [1]-' \ 0 '));
If (I <0xb0a1) Return "*";
If (I <0xb0c5) Return "";
If (I <0xb2c1) Return "B ";
If (I <0xb4ee) Return "C ";
If (I <0xb6ea) Return "D ";
If (I <0xb7a2) Return "E ";
If (I <0xb8c1) Return "F ";
If (I <0xb9fe) Return "G ";
If (I <0xbbf7) Return "H ";
If (I <0xbfa6) Return "G ";
If (I <0xc0ac) Return "K ";
If (I <0xc2e8) Return "L ";
If (I <0xc4c3) Return "M ";
If (I <0xc5b6) Return "N ";
If (I <0xc5be) Return "O ";
If (I <0xc6da) Return "p ";
If (I <0xc8bb) Return "Q ";
If (I <0xc8f6) Return "R ";
If (I <0 xcbfa) Return "S ";
If (I <0 xcdda) Return "T ";
If (I <0xcef4) Return "W ";
If (I <0xd1b9) Return "X ";
If (I <0xd4d1) Return "Y ";
If (I <0xd7fa) Return "Z ";
Return "*";
}
}
C # obtain the pinyin initials of a single character