Decodes the specified HTTP parameter string to an Ascii85 string. The results of this function would normally be fed to Ascii85.Decode(). - CSharp System

CSharp examples for System:String Encode Decode

Description

Decodes the specified HTTP parameter string to an Ascii85 string. The results of this function would normally be fed to Ascii85.Decode().

Demo Code


using System.Text;
using System;//ww  w .j  a  v a  2 s. co m

public class Main{
        /// <summary>
        /// Decodes the specified HTTP parameter string to an Ascii85 string.
        /// The results of this function would normally be fed to Ascii85.Decode().
        /// </summary>
        /// <param name="encoded">The HTTP parameter string</param>
        /// <returns>The Ascii85 string.</returns>
        public static string Decode(string encoded)
        {
            if (encoded == null)
                throw new ArgumentNullException("encoded");

            // The decoded string will not be longer than the encoded string.
            StringBuilder sb = new StringBuilder(encoded.Length);

            // Walk the input string.

            int i = 0;
            while (i < encoded.Length)
            {
                switch (encoded[i])
                {
                    case 'v':
                        sb.Append('(');
                        break;
                    case 'w':
                        sb.Append(')');
                        break;
                    case 'x':
                        sb.Append('<');
                        break;
                    case 'y':
                        sb.Append('>');
                        break;
                    case '|':
                        sb.Append('@');
                        break;
                    case ' ':
                    case '\n':
                    case '\r':
                    case '\t':
                        // Pass white space through unchanged.
                        sb.Append(encoded[i]);
                        break;
                    case '~':
                        i++;
                        if (i >= encoded.Length)
                            throw new FormatException("'~' cannot be the last character of the encoded string.");
                        switch (encoded[i])
                        {
                            case 'a':
                                sb.Append(',');
                                break;
                            case 'b':
                                sb.Append(';');
                                break;
                            case 'c':
                                sb.Append(':');
                                break;
                            case 'd':
                                sb.Append('\\');
                                break;
                            case 'e':
                                sb.Append('"');
                                break;
                            case 'f':
                                sb.Append('/');
                                break;
                            case 'g':
                                sb.Append('[');
                                break;
                            case 'h':
                                sb.Append(']');
                                break;
                            case 'i':
                                sb.Append('?');
                                break;
                            case 'j':
                                sb.Append('=');
                                break;
                            default:
                                throw new FormatException("Unexpected character following '~': '{0}'".FormatInvariant(encoded[i]));
                        }
                        break;
                    default:
                        sb.Append(encoded[i]);
                        break;
                }
                i++;
            }
            return sb.ToString();
        }
}

Related Tutorials