c# UrlCode

public string UrlEncode(string txt)

        {

            StringBuilder sb = new StringBuilder();

            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(txt); //默认是System.Text.Encoding.Default.GetBytes(str)

            for (int i = 0; i < byStr.Length; i++)

            {

                sb.Append(@"%" + Convert.ToString(byStr[i], 16));

            }

            return (sb.ToString());

        }



        public string Urlcode(string txt)

        {

            string[] values = txt.Split('%');

            StringBuilder sb = new StringBuilder();

            byte[] bytes = new byte[values.Length - 1];

            for (int i = 1; i < values.Length; i++)

            {

                bytes[i - 1] = Convert.ToByte(values[i], 16);

            }

            sb.Append(System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length));

            return (sb.ToString());

        }
/// <summary>

        /// 为了保密,在将文字信息保存到XML文件中之前,先将要保密的信息进行加密处理

        /// </summary>

        /// <param name="v">要加密的文字信息</param>

        /// <returns>已加密的文字信息</returns>

        public string encrypt(string v)

        {

            string ev = "";

            try

            {

                byte[] bs = StringUtility.StringToBytes(Encoding.Unicode, v);

                for (int i = 0; i < bs.Length; i++)

                {

                    int n = int.Parse(bs[i].ToString("x"), NumberStyles.HexNumber);

                    ev += "/" + (255 + 17 - n);

                }

                ev = (ev.Length > 1 ? ev.Substring(1) : "");

            }

            catch { ev = ""; }

            return ev;

        }



        /// <summary>

        /// 在将加密文字信息从XML文件中取出之后,应进行解密处理

        /// </summary>

        /// <param name="ev">要解密的文字信息</param>

        /// <returns>已解密的文字信息</returns>

        public string unchain(string ev)

        {

            string v = "";

            try

            {

                string[] ss = StringUtility.Split(ev, "/");

                byte[] bs = new byte[ss.Length];

                for (int i = 0; i < ss.Length; i++)

                {

                    int n = int.Parse(ss[i]);

                    bs[i] = (byte)(255 + 17 - n);

                }

                v = StringUtility.BytesToString(Encoding.Unicode, bs);

            }

            catch { v = ""; }

            return v;

        }



/// <summary>

        /// 按指定的字符编码,把字节数组转换成字符串

        /// </summary>

        /// <param name="ec">字符编码。</param>

        /// <param name="bs">字节数组</param>

        /// <returns></returns>

        public static string BytesToString(Encoding ec, byte[] bs)

        {

            char[] cs = new char[ec.GetCharCount(bs, 0, bs.Length)];

            ec.GetChars(bs, 0, bs.Length, cs, 0);

            string rs = new string(cs);

            return rs;

        }



        /// <summary>

        /// 按缺省的字符编码,把字节数组转换成字符串

        /// </summary>

        /// <param name="bs"></param>

        /// <returns></returns>

        public static string BytesToString(byte[] bs)

        {

            return BytesToString(Encoding.Default, bs);

        }
 
   

 

 
  

 

你可能感兴趣的:(code)