半角/全角互换的代码

       最近做一个项目,中间有蓝牙打印部分,可能是老外不重视中国市场的原因,对于中文和英文的混打没有作处理。无奈之举只好做了如下转换程序,把需要打印的内容全部转换成中文(全角):

 /// <summary>
       /// 全角转半角
       /// </summary>
       /// <param name="QJstr"></param>
       /// <returns></returns>
       public static string GetBanJiao(string QJstr)
       {
           char[] c = QJstr.ToCharArray();
           for (int i = 0; i < c.Length; i++)
           {
               byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
               if (b.Length == 2)
               {
                   if (b[1] == 255)
                   {
                       b[0] = (byte)(b[0] + 32);
                       b[1] = 0;
                       c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
                   }
               }
           }
           return new string(c);
       }
       /// <summary>
       /// 半角转全角
       /// </summary>
       /// <param name="QJstr"></param>
       /// <returns></returns>
       public static string GetQuanJiao(string QJstr)
       {
           char[] c = QJstr.ToCharArray();
           for (int i = 0; i < c.Length; i++)
           {
               if (c[i] == ' ')
                   c[i] = ' ';

               byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
               if (b.Length == 2)
               {
                   if (b[1] == 0)
                   {
                       b[0] = (byte)(b[0] - 32);
                       b[1] = 255;
                       c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
                   }
               }
           }
           return new string(c);
       }

你可能感兴趣的:(代码)