天天看点

半角与全角之间的转化[转]

全角 半角

一、半角与全角之间的转化

    /// <summary>

    /// 半角转全角

    /// </summary>

    /// <param name="BJstr"></param>

    /// <returns></returns>

    static public string GetQuanJiao(string BJstr)

    {

        char[] c = BJstr.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] == 0)

                {

                    b[0] = (byte)(b[0] - 32);

                    b[1] = 255;

                    c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];

                }

            }

        }

        string strNew = new string(c);

        return strNew;

    }

    /// 全角转半角

    /// <param name="QJstr"></param>

    static public string GetBanJiao(string QJstr)

        char[] c = QJstr.ToCharArray();

                if (b[1] == 255)

                    b[0] = (byte)(b[0] + 32);

                    b[1] = 0;