public class TransferCode
{
#region base-64编码、解码 /// <summary>
/// BASE64编码
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string Base64Encode(string s)
{
try
{
s = Convert.ToBase64String(Encoding.Default.GetBytes(s));
return Convert.ToBase64String(Encoding.Default.GetBytes(s));
}
catch
{
return null;
}
} /// <summary>
/// BASE64解码
/// </summary>
/// <param name="s"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string Base64Decode(string s)
{
try
{
s = Encoding.Default.GetString(Convert.FromBase64String(s));
return Encoding.Default.GetString(Convert.FromBase64String(s));
}
catch
{
return null;
}
} #endregion #region url编码、解码
/// <summary>
/// URL编码
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string UrlEncode(string s)
{
return System.Web.HttpUtility.UrlEncode(s);
}
/// <summary>
/// URL解码
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string UrlDecode(string s)
{
return System.Web.HttpUtility.UrlDecode(s);
}
#endregion /// <summary>
/// <summary>
/// 字符串转Unicode
/// </summary>
/// <param name="source">源字符串</param>
/// <returns>Unicode编码后的字符串</returns>
public static string String2Unicode(string source)
{
byte[] bytes = Encoding.Unicode.GetBytes(source);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < bytes.Length; i += 2)
{
stringBuilder.AppendFormat("\\u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
}
return stringBuilder.ToString();
}
}