全角转半角(C#,unicode)

 /*全角字符从的unicode编码从65281~65374   
      半角字符从的unicode编码从               33~126   
     * 差值65248
      空格比较特殊,全角为       12288,半角为       32 
     
*/
    
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);
    }

posted @ 2008-12-05 10:22 军军 阅读(454) 评论(0) 编辑 收藏