Beispiel #1
0
        /// <summary>
        /// Encodes a URL, like System.Web.HttpUtility.UrlEncode
        /// </summary>
        /// <returns>The decoded URL</returns>
        /// <param name="url">The URL fragment to decode</param>
        /// <param name="encoding">The encoding to use</param>
        public static string UrlDecode(string value, System.Text.Encoding encoding = null)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            encoding = encoding ?? System.Text.Encoding.UTF8;

            var decoder = encoding.GetDecoder();
            var inbuf = new byte[2];
            var outbuf = new char[1];

            return RE_NUMBER.Replace(value, (m) => {
                if (m.Value == "+")
                    return " ";

                try
                {
                    var hex = m.Groups["number"].Value;
                    Utility.HexStringAsByteArray(hex, inbuf);

                    decoder.GetChars(inbuf, 0, hex.Length / 2, outbuf, 0);
                    return outbuf[0].ToString();
                }
                catch
                {
                }

                //Fallback
                return m.Value;
            });
        }
 private static string ConvertEncoding(string value, System.Text.Encoding src, System.Text.Encoding trg)
 {
     System.Text.Decoder decoder = src.GetDecoder();
     byte[] bytes = trg.GetBytes(value);
     char[] chars = new char[decoder.GetCharCount(bytes, 0, bytes.Length)];
     decoder.GetChars(bytes, 0, bytes.Length, chars, 0);
     return new string(chars);
 }