using System;
using System.Text;namespace StockSellStorage.Business
{
//该类是用来得到汉字的拼音码。
public class ChineseConvertor
{
private ChineseConvertor() { } /// <summary>
/// 获取一串汉字的拼音声母
/// </summary>
/// <param name="chinese">Unicode格式的汉字字符串</param>
/// <returns>拼音声母字符串</returns>
/// <example>
/// “新桥软件”转换为“xqrj”
/// </example>
public static String Convert(String chinese)
{
char[] buffer = new char[chinese.Length];
for (int i = 0; i < chinese.Length; i++)
{
buffer[i] = Convert(chinese[i]);
}
return new String(buffer);
} /// <summary>
/// 获取一个汉字的拼音声母
/// </summary>
/// <param name="chinese">Unicode格式的一个汉字</param>
/// <returns>汉字的声母</returns>
public static char Convert(Char chinese)
{
Encoding gb2312 = Encoding.GetEncoding("GB2312");
Encoding unicode = Encoding.Unicode; // Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(new Char[] { chinese });
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, gb2312, unicodeBytes); // 计算该汉字的GB-2312编码
int n = (int)asciiBytes[0] << 8;
n += (int)asciiBytes[1]; // 根据汉字区域码获取拼音声母
if (In(0xB0A1, 0xB0C4, n)) return 'a';
if (In(0XB0C5, 0XB2C0, n)) return 'b';
if (In(0xB2C1, 0xB4ED, n)) return 'c';
if (In(0xB4EE, 0xB6E9, n)) return 'd';
if (In(0xB6EA, 0xB7A1, n)) return 'e';
if (In(0xB7A2, 0xB8c0, n)) return 'f';
if (In(0xB8C1, 0xB9FD, n)) return 'g';
if (In(0xB9FE, 0xBBF6, n)) return 'h';
if (In(0xBBF7, 0xBFA5, n)) return 'j';
if (In(0xBFA6, 0xC0AB, n)) return 'k';
if (In(0xC0AC, 0xC2E7, n)) return 'l';
if (In(0xC2E8, 0xC4C2, n)) return 'm';
if (In(0xC4C3, 0xC5B5, n)) return 'n';
if (In(0xC5B6, 0xC5BD, n)) return 'o';
if (In(0xC5BE, 0xC6D9, n)) return 'p';
if (In(0xC6DA, 0xC8BA, n)) return 'q';
if (In(0xC8BB, 0xC8F5, n)) return 'r';
if (In(0xC8F6, 0xCBF0, n)) return 's';
if (In(0xCBFA, 0xCDD9, n)) return 't';
if (In(0xCDDA, 0xCEF3, n)) return 'w';
if (In(0xCEF4, 0xD188, n)) return 'x';
if (In(0xD1B9, 0xD4D0, n)) return 'y';
if (In(0xD4D1, 0xD7F9, n)) return 'z';
return '\0';
} private static bool In(int Lp, int Hp, int Value)
{
return ((Value <= Hp) && (Value >= Lp));
} /// <summary>
/// 判断输入的字符是否为汉字
/// </summary>
/// <param name="content">被测的字符</param>
/// <returns>bool类型是汉字返回true,否则返回false</returns>
public static bool IsChineseCharacter(char content)
{
if (Convert(content) != '\0')
{
return true;
}
else
{
return false;
}
}
}
}//下面是我在一个文本框的KeyPress事件中的代码。
private void txtSName_KeyPress(object sender, KeyPressEventArgs e)
{
//判断是否按下了退格键,如果按下了退格键的话就把下面的文本框也相应的清除一个拼音。
if ((int)e.KeyChar == 8)
{
//把文本框内的内容保存为字符数组。
char[] ch = txtSName.Text.ToCharArray();
//判断输入的内容是否为汉字,注意汉字的的编码比ascii码大得多。
if ((int)ch[ch.Length - 1] > 128)
{
//判断被退格的字符是否为汉字。注:也许不是汉字例如是中文的标点符号。
if (ChineseConvertor.IsChineseCharacter(ch[ch.Length - 1]))
{
//清除txtSpell文本框内最后一个拼音码。
txtSpell.Text = txtSpell.Text.Substring(0, txtSpell.Text.Length - 1);
}
}
}
//如果输入的是汉字就把它显示在txtSpell当中。
if ((int)e.KeyChar > 128)
{
txtSpell.Text += ChineseConvertor.Convert(e.KeyChar);
//e.Handled = true;
}
}我现在想在文本框txtSName内输入汉字时,在下面的文本框txtSpell当中显示出对应的拼音码,但是在Vs2005当中拼音码却出现了两遍(在Vs2003当中没事)。这怎么解决呢?
using System.Text;namespace StockSellStorage.Business
{
//该类是用来得到汉字的拼音码。
public class ChineseConvertor
{
private ChineseConvertor() { } /// <summary>
/// 获取一串汉字的拼音声母
/// </summary>
/// <param name="chinese">Unicode格式的汉字字符串</param>
/// <returns>拼音声母字符串</returns>
/// <example>
/// “新桥软件”转换为“xqrj”
/// </example>
public static String Convert(String chinese)
{
char[] buffer = new char[chinese.Length];
for (int i = 0; i < chinese.Length; i++)
{
buffer[i] = Convert(chinese[i]);
}
return new String(buffer);
} /// <summary>
/// 获取一个汉字的拼音声母
/// </summary>
/// <param name="chinese">Unicode格式的一个汉字</param>
/// <returns>汉字的声母</returns>
public static char Convert(Char chinese)
{
Encoding gb2312 = Encoding.GetEncoding("GB2312");
Encoding unicode = Encoding.Unicode; // Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(new Char[] { chinese });
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, gb2312, unicodeBytes); // 计算该汉字的GB-2312编码
int n = (int)asciiBytes[0] << 8;
n += (int)asciiBytes[1]; // 根据汉字区域码获取拼音声母
if (In(0xB0A1, 0xB0C4, n)) return 'a';
if (In(0XB0C5, 0XB2C0, n)) return 'b';
if (In(0xB2C1, 0xB4ED, n)) return 'c';
if (In(0xB4EE, 0xB6E9, n)) return 'd';
if (In(0xB6EA, 0xB7A1, n)) return 'e';
if (In(0xB7A2, 0xB8c0, n)) return 'f';
if (In(0xB8C1, 0xB9FD, n)) return 'g';
if (In(0xB9FE, 0xBBF6, n)) return 'h';
if (In(0xBBF7, 0xBFA5, n)) return 'j';
if (In(0xBFA6, 0xC0AB, n)) return 'k';
if (In(0xC0AC, 0xC2E7, n)) return 'l';
if (In(0xC2E8, 0xC4C2, n)) return 'm';
if (In(0xC4C3, 0xC5B5, n)) return 'n';
if (In(0xC5B6, 0xC5BD, n)) return 'o';
if (In(0xC5BE, 0xC6D9, n)) return 'p';
if (In(0xC6DA, 0xC8BA, n)) return 'q';
if (In(0xC8BB, 0xC8F5, n)) return 'r';
if (In(0xC8F6, 0xCBF0, n)) return 's';
if (In(0xCBFA, 0xCDD9, n)) return 't';
if (In(0xCDDA, 0xCEF3, n)) return 'w';
if (In(0xCEF4, 0xD188, n)) return 'x';
if (In(0xD1B9, 0xD4D0, n)) return 'y';
if (In(0xD4D1, 0xD7F9, n)) return 'z';
return '\0';
} private static bool In(int Lp, int Hp, int Value)
{
return ((Value <= Hp) && (Value >= Lp));
} /// <summary>
/// 判断输入的字符是否为汉字
/// </summary>
/// <param name="content">被测的字符</param>
/// <returns>bool类型是汉字返回true,否则返回false</returns>
public static bool IsChineseCharacter(char content)
{
if (Convert(content) != '\0')
{
return true;
}
else
{
return false;
}
}
}
}//下面是我在一个文本框的KeyPress事件中的代码。
private void txtSName_KeyPress(object sender, KeyPressEventArgs e)
{
//判断是否按下了退格键,如果按下了退格键的话就把下面的文本框也相应的清除一个拼音。
if ((int)e.KeyChar == 8)
{
//把文本框内的内容保存为字符数组。
char[] ch = txtSName.Text.ToCharArray();
//判断输入的内容是否为汉字,注意汉字的的编码比ascii码大得多。
if ((int)ch[ch.Length - 1] > 128)
{
//判断被退格的字符是否为汉字。注:也许不是汉字例如是中文的标点符号。
if (ChineseConvertor.IsChineseCharacter(ch[ch.Length - 1]))
{
//清除txtSpell文本框内最后一个拼音码。
txtSpell.Text = txtSpell.Text.Substring(0, txtSpell.Text.Length - 1);
}
}
}
//如果输入的是汉字就把它显示在txtSpell当中。
if ((int)e.KeyChar > 128)
{
txtSpell.Text += ChineseConvertor.Convert(e.KeyChar);
//e.Handled = true;
}
}我现在想在文本框txtSName内输入汉字时,在下面的文本框txtSpell当中显示出对应的拼音码,但是在Vs2005当中拼音码却出现了两遍(在Vs2003当中没事)。这怎么解决呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货