代码改变世界

全角转半角(C#,unicode)

2010-12-22 15:45  robinli  阅读(232)  评论(0)    收藏  举报
响应KeyPress事件
 

    
public char FullCodeToHalfCode(char c)
    {
        
//得到c的编码
        byte[] bytes = System.Text.Encoding.Unicode.GetBytes(c.ToString());

        
int = Convert.ToInt32(bytes[1]);
        
int = Convert.ToInt32(bytes[0]);

        
//得到unicode编码
        int value = * 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);
    }