响应KeyPress事件
public char FullCodeToHalfCode(char c)
{
//得到c的编码
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(c.ToString());
int H = Convert.ToInt32(bytes[1]);
int L = Convert.ToInt32(bytes[0]);
//得到unicode编码
int value = H * 256 + L;
//是全角
if (value >= 65281 && value <= 65374)
{
int halfvalue = value - 65248;//65248是全半角间的差值。
byte halfL = Convert.ToByte(halfvalue);
bytes[0] = halfL;
bytes[1] = 0;
}
else if (value == 12288)
{
int halfvalue = 32;
byte halfL = Convert.ToByte(halfvalue);
bytes[0] = halfL;
bytes[1] = 0;
}
else
{
return c;
}
//将bytes转换成字符
string ret = System.Text.Encoding.Unicode.GetString(bytes);
return Convert.ToChar(ret);
}