c# /u4E2D/u56FD解码编码

warning: 这篇文章距离上次修改已过566天,其中的内容可能已经有所变动。

c# /u4E2D/u56FD解码编码

可编码解码类似于这种变态的字符串%u4E2D%u56FD

Powered BY Error Q:302777528

 

   /// <summary>
        /// 使用UCS2进行编码,用于发送Unicode字符
        /// </summary>
        /// <param name="str">短信内容</param>
        /// <returns>编码结果</returns>
    public static   string encodeUnicode(string str)
        {
            Encoding encodingUTF = Encoding.BigEndianUnicode;
            string strDestination = string.Empty;
            byte[] encodedBytes = encodingUTF.GetBytes(str);
            for (int i = 0; i < encodedBytes.Length; i++)
            {
                strDestination += BitConverter.ToString(encodedBytes, i, 1);
            }

    /// <summary>
        /// url编码
        /// </summary>
        /// <param name="content">要编码的内容</param>
        /// <param name="lx">类型0:utf-8 1:GB2312 2:GBK</param>
        /// <returns></returns>
        public static string myEncoding(string content, int lx)
        {
            if (lx == 0)
            {
                return HttpUtility.UrlEncode(content, Encoding.GetEncoding("UTF-8"));
            }
            else if (lx == 1)
            {
                return HttpUtility.UrlEncode(content, Encoding.GetEncoding("GB2312"));
            }

    /// <summary>
        /// 对UCS2编码进行解码
        /// </summary>
        /// <param name="s">解码字符串</param>
        /// <returns>解码结果</returns>
        public static string decodingUnicode(string s)
        {
            byte[] buf = new byte[s.Length];
            for (int i = 0; i < s.Length; i += 4)
            {
              buf[i / 2] = byte.Parse(s.Substring(2 + i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
              buf[i / 2 + 1] = byte.Parse(s.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            }

 

none
最后修改于:2023年05月08日 07:55

评论已关闭