C# 全半角转换

 

 1    /// <summary>
 2            /// 转全角的函数(SBC case)
 3            /// </summary>
 4            /// <param name="input">任意字符串</param>
 5            /// <returns>全角字符串</returns>
 6            ///<remarks>
 7            ///全角空格为12288,半角空格为32
 8            ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
 9            ///</remarks>        

10              public   static   string  ToSBC( string  input)
11              {
12                //半角转全角:
13                char[] c = input.ToCharArray();
14                for (int i = 0; i < c.Length; i++)
15                {
16                    if (c[i] == 32)
17                    {
18                        c[i] = (char)12288;
19                        continue;
20                    }

21                    if (c[i] < 127)
22                        c[i] = (char)(c[i] + 65248);
23                }

24                return new string(c);
25            }

26
27
28              /// <summary>
29            /// 转半角的函数(DBC case)
30            /// </summary>
31            /// <param name="input">任意字符串</param>
32            /// <returns>半角字符串</returns>
33            ///<remarks>
34            ///全角空格为12288,半角空格为32
35            ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
36            ///</remarks>

37              public   static   string  ToDBC( string  input)
38              {
39                char[] c = input.ToCharArray();
40                for (int i = 0; i < c.Length; i++)
41                {
42                    if (c[i] == 12288)
43                    {
44                        c[i] = (char)32;
45                        continue;
46                    }

47                    if (c[i] > 65280 && c[i] < 65375)
48                        c[i] = (char)(c[i] - 65248);
49                }

50                return new string(c);
51            }

你可能感兴趣的:(C#)