//字节数组转16进制字符串
public static string ByteToHexString(byte[] bytes)
{
return BitConverter.ToString(bytes).Replace("-", "");
}
//16进制字符串转字节数组
public static byte[] HexStringToByte(string hexStr)
{
hexStr = hexStr.Replace(" ", "");
if ((hexStr.Length % 2) != 0)
hexStr += " ";
byte[] returnBytes = new byte[hexStr.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexStr.Substring(i * 2, 2), 16);
return returnBytes;
}
/// <summary>
/// 字符串转16进制字符串
/// </summary>
/// <param name="s"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string StringToHexString(string str)
{
byte[] bytes = Encoding.ASCII.GetBytes(str);
return ByteToHexString(bytes);
}
/// <summary>
/// 将16进制字符串转为字符串
/// </summary>
/// <param name="hs"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string HexStringToString(string HexStr)
{
var bytes=HexStringToByte(HexStr);
return Encoding.ASCII.GetString(bytes);
}
网友评论