System.Web.HttpServerUtility.UrlTokenEncode C# (CSharp) Method

UrlTokenEncode() public static method

public static UrlTokenEncode ( byte input ) : string
input byte
return string
		public static string UrlTokenEncode (byte[] input)
		{
			if (input == null)
				throw new ArgumentNullException ("input");
			if (input.Length < 1)
				return String.Empty;
			string base64 = Convert.ToBase64String (input);
			int retlen;
			if (base64 == null || (retlen = base64.Length) == 0)
				return String.Empty;

			// MS.NET implementation seems to process the base64
			// string before returning. It replaces the chars:
			//
			//  + with -
			//  / with _
			//
			// Then removes trailing ==, which may appear in the
			// base64 string, and replaces them with a single digit
			// that's the count of removed '=' characters (0 if none
			// were removed)
			int equalsCount = 0x30;
			while (retlen > 0 && base64[retlen - 1] == '=') {
				equalsCount++;
				retlen--;
			}
			char[] chars = new char[retlen + 1];
			chars[retlen] = (char)equalsCount;
			for (int i = 0; i < retlen; i++) {
				switch (base64[i]) {
					case '+':
						chars[i] = '-';
						break;

					case '/':
						chars[i] = '_';
						break;
					
					default:
						chars[i] = base64[i];
						break;
				}
			}
			return new string (chars);
		}
#endif

Usage Example

Example #1
0
 /// <summary>
 /// Encodes an URI token from the byte array.
 /// </summary>
 /// <param name="input">The input string to encode.</param>
 /// <returns>Returns with an URI token.</returns>
 public static string EncodeUriTokenBytes(byte[] input)
 {
     return(HttpServerUtility.UrlTokenEncode(input));
 }
All Usage Examples Of System.Web.HttpServerUtility::UrlTokenEncode