/// <summary>
/// 编码
/// </summary>
/// <param name="s"></param>
/// <param name="encode"></param>
/// <returns></returns>
private string StringToHexString(string s, Encoding encode)
{
byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
string result = string.Empty;
for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符,以%隔开
{
string t = Convert.ToString(s[i], 16);
result += @"\u" + t.PadLeft(4, '0');
}
return result;
}
/// <summary>
/// 解码
/// </summary>
/// <param name="hs"></param>
/// <param name="encode"></param>
/// <returns></returns>
private string HexStringToString(string hs, Encoding encode)
{
hs = hs.Replace(@"\u","%");
string[] chars = hs.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries);
byte[] b = new byte[chars.Length];
//逐个字符变为16进制字节数据
for (int i = 0; i < chars.Length; i++)
{
b[i] = Convert.ToByte(chars[i], 16);
}
//按照指定编码将字节数组变为字符串
return encode.GetString(b);
}
/// 编码
/// </summary>
/// <param name="s"></param>
/// <param name="encode"></param>
/// <returns></returns>
private string StringToHexString(string s, Encoding encode)
{
byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
string result = string.Empty;
for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符,以%隔开
{
string t = Convert.ToString(s[i], 16);
result += @"\u" + t.PadLeft(4, '0');
}
return result;
}
/// <summary>
/// 解码
/// </summary>
/// <param name="hs"></param>
/// <param name="encode"></param>
/// <returns></returns>
private string HexStringToString(string hs, Encoding encode)
{
hs = hs.Replace(@"\u","%");
string[] chars = hs.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries);
byte[] b = new byte[chars.Length];
//逐个字符变为16进制字节数据
for (int i = 0; i < chars.Length; i++)
{
b[i] = Convert.ToByte(chars[i], 16);
}
//按照指定编码将字节数组变为字符串
return encode.GetString(b);
}