Encodes the specified Ascii85 string for safe passage in an HTTP header by substituting other characters for characters used by Ascii85 in the set to avoid the set of separator characters not allowed in HTTP headers, which is {()<>@,;:\"[]?=}. The curly braces aren't used by Ascii85, but cannot be used in HTTP headers, either. Since there are 15 disallowed characters, and only 6 printable ASCII characters that are both allowed in HTTP headers and not used by Ascii85, one of those (~) is used as an escape character to introduce a two-character set to represent each of 10 of those disallowed sequences. This function would normally be used to encode the results of Ascii85.Encode(). - CSharp System

CSharp examples for System:String Encode Decode

Description

Encodes the specified Ascii85 string for safe passage in an HTTP header by substituting other characters for characters used by Ascii85 in the set to avoid the set of separator characters not allowed in HTTP headers, which is {()<>@,;:\"[]?=}. The curly braces aren't used by Ascii85, but cannot be used in HTTP headers, either. Since there are 15 disallowed characters, and only 6 printable ASCII characters that are both allowed in HTTP headers and not used by Ascii85, one of those (~) is used as an escape character to introduce a two-character set to represent each of 10 of those disallowed sequences. This function would normally be used to encode the results of Ascii85.Encode().

Demo Code


using System.Text;
using System;/*  ww w.j  ava 2 s  .co m*/

public class Main{
        /// <summary>
        /// Encodes the specified Ascii85 string for safe passage in an HTTP header by
        /// substituting other characters for characters used by Ascii85 in the set to
        /// avoid the set of separator characters not allowed in HTTP headers, which is
        /// {()&lt;&gt;@,;:\"/[]?=}. The curly braces aren't used by Ascii85, but cannot be
        /// used in HTTP headers, either. Since there are 15 disallowed characters, and only
        /// 6 printable ASCII characters that are both allowed in HTTP headers and not used
        /// by Ascii85, one of those (~) is used as an escape character to introduce a
        /// two-character set to represent each of 10 of those disallowed sequences.
        /// 
        /// This function would normally be used to encode the results of Ascii85.Encode().
        ///  
        /// </summary>
        /// <param name="ascii85">A valid Ascii85 string.</param>
        /// <returns>A string that is valid as an HTTP header.</returns>
        public static string Encode(string ascii85)
        {
            if (ascii85 == null)
                throw new ArgumentNullException("ascii85");

            // Instantiate a StringBuilder with more than enough room to store the new string.
            StringBuilder sb = new StringBuilder(ascii85.Length * 2);

            // walk the characters
            foreach (char ch in ascii85)
            {
                switch (ch)
                {
                    case '(':
                        sb.Append('v');
                        break;
                    case ')':
                        sb.Append('w');
                        break;
                    case '<':
                        sb.Append('x');
                        break;
                    case '>':
                        sb.Append('y');
                        break;
                    case '@':
                        sb.Append('|');
                        break;
                    case ',':
                        sb.Append("~a");
                        break;
                    case ';':
                        sb.Append("~b");
                        break;
                    case ':':
                        sb.Append("~c");
                        break;
                    case '\\':
                        sb.Append("~d");
                        break;
                    case '"':
                        sb.Append("~e");
                        break;
                    case '/':
                        sb.Append("~f");
                        break;
                    case '[':
                        sb.Append("~g");
                        break;
                    case ']':
                        sb.Append("~h");
                        break;
                    case '?':
                        sb.Append("~i");
                        break;
                    case '=':
                        sb.Append("~j");
                        break;
                    case ' ':
                    case '\n':
                    case '\r':
                    case '\t':
                    case 'z':
                        // Pass white space and z through unchanged.
                        sb.Append(ch);
                        break;
                    default:
                        if ((ch < c_firstCharacter) || (ch > c_lastCharacter))
                            throw new FormatException("Invalid character '{0}' in Ascii85 string.".FormatInvariant(ch));
                        else
                            sb.Append(ch);
                        break;
                }
            }
            return sb.ToString();
        }
}

Related Tutorials