全角转半角

 

//全角字符从的unicode编码从65281~65374  

  //   半角字符从的unicode编码从  33~126   

 //    * 差值65248 

 //   空格比较特殊,全角为       12288,半角为       32     

   public char FullCode2HalfCode(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 @ 2010-11-02 12:58  *天道酬勤*  阅读(279)  评论(0编辑  收藏  举报