Decode and Encode string in C#
/// Encode a given text
/// </summary>
/// <param name="textToEncode">Text to encode</param>
/// <returns>Encoded Text</returns>
public static string EncodeText(string textToEncode)
{
if (string.IsNullOrEmpty(textToEncode)) return string.Empty;
byte[] byteBuff = System.Text.Encoding.UTF8.GetBytes(textToEncode);
string retval = Convert.ToBase64String(byteBuff);
retval = retval.Replace("=", "repeq");
return retval;
}
/// <summary>
/// Decode a set of given encoded text
/// </summary>
/// <param name="encodedText">Encoded Text</param>
/// <returns>Decoded Text</returns>
public static string DecodeText(string encodedText)
{
if (encodedText == null) return string.Empty;
encodedText = encodedText.Replace("repeq", "=");
byte[] byteBuff = Convert.FromBase64String(encodedText);
string retval = System.Text.Encoding.UTF8.GetString(byteBuff, 0, byteBuff.Length);
return retval;
}
Forget about the replace used in the above example; this is just a test;
This can be used while sending text as initparams in silverlight
thanks a lot for this tip of the week, worked like a charm! thumps up
ReplyDelete