Unicode加解码
/// <summary>
/// 使用UCS2进行编码,用于发送Unicode字符
/// </summary>
/// <param name="str">短信内容</param>
/// <returns>编码结果</returns>
public static string encodeUnicode(string str)
{
Encoding encodingUTF = Encoding.BigEndianUnicode;
string strDestination = string.Empty;
byte[] encodedBytes = encodingUTF.GetBytes(str);
for (int i = 0; i < encodedBytes.Length; i++)
{
strDestination += BitConverter.ToString(encodedBytes, i, 1);
}
/// <summary>
/// url编码
/// </summary>
/// <param name="content">要编码的内容</param>
/// <param name="lx">类型0:utf-8 1:GB2312 2:GBK</param>
/// <returns></returns>
public static string myEncoding(string content, int lx)
{
if (lx == 0)
{
return HttpUtility.UrlEncode(content, Encoding.GetEncoding("UTF-8"));
}
else if (lx == 1)
{
return HttpUtility.UrlEncode(content, Encoding.GetEncoding("GB2312"));
}
/// <summary>
/// 对UCS2编码进行解码
/// </summary>
/// <param name="s">解码字符串</param>
/// <returns>解码结果</returns>
public static string decodingUnicode(string s)
{
byte[] buf = new byte[s.Length];
for (int i = 0; i < s.Length; i += 4)
{
buf[i / 2] = byte.Parse(s.Substring(2 + i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
buf[i / 2 + 1] = byte.Parse(s.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}