/// <summary> /// Encode the array of characters. /// </summary> /// <param name="data">The array of characters.</param> /// <returns>The encoded string.</returns> public static string Encode(char[] data) { int ascii; string encodedChar = ""; StringBuilder builder = new StringBuilder(); // For each char. for (int i = 0; i < data.Length; i++) { ascii = Convert.ToInt32(data[i]); if (ascii < 32 || ascii > 126 || ascii == 61) { encodedChar = HexEncoder.ToHexString(new byte[] { Convert.ToByte(data[i]) }); if (encodedChar.Length == 1) { encodedChar = "0" + encodedChar; } builder.Append("=" + encodedChar.ToUpper()); } else { builder.Append(data[i]); } } // Return the encoded string return(builder.ToString()); }
/// <summary> /// Decode the array of characters. /// </summary> /// <param name="data">The array of characters.</param> /// <returns>The decoded string.</returns> public static string Decode(char[] data) { StringBuilder builder = new StringBuilder(); // For each char. for (int i = 0; i < data.Length; i++) { if (data[i] == '=') { string value = null; if (data[i + 1] == '0') { value = data[i + 2].ToString(); } else { value = data[i + 1].ToString() + data[i + 2].ToString(); } int intValue = Convert.ToInt32("0x" + value); if (value == HexEncoder.ToHexString(new byte[] { Convert.ToByte(intValue) })) { builder.Append(Convert.ToChar(intValue)); i += 2; } else { builder.Append(data[i]); } } else { builder.Append(data[i]); } } // Return the encoded string return(builder.ToString()); }