编码:
using System.Text;
byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes("中国");
string str = "";
foreach (byte b in buffer) str += string.Format("%{0:X}", b);
解码:
public static string Decode(string str)
{
string[] strs = str.Split('%');
List<byte> list = new List<byte>();
for (int i = 0; i < strs.Length; i++)
{
if (strs[i] == "") continue;
int a = Int32.Parse(strs[i].ToLower(), System.Globalization.NumberStyles.AllowHexSpecifier);
//将十六进制转换成十进制
list.Add((byte)a);
}
return CharsToString(System.Text.Encoding.UTF8.GetChars(list.ToArray()));
}