#region 字节型转换16
///
/// 把字节型转换成十六进制字符串
///
///
///
public static string ByteToString(byte[] InBytes)
{
string StringOut = "";
foreach (byte InByte in InBytes)
{
StringOut = StringOut + String.Format("{0:X2} ", InByte);
}
return StringOut;
}
#endregion
#region 十六进制字符串转字节型
///
/// 把十六进制字符串转换成字节型(方法1)
///
///
///
public static byte[] StringToByte(string InString)
{
string[] ByteStrings;
ByteStrings = InString.Split(" ".ToCharArray());
byte[] ByteOut;
ByteOut = new byte[ByteStrings.Length];
for (int i &#61; 0; i <&#61; ByteStrings.Length - 1; i&#43;&#43;)
{
//ByteOut[i] &#61; System.Text.Encoding.ASCII.GetBytes(ByteStrings[i]);
ByteOut[i] &#61; Byte.Parse(ByteStrings[i], System.Globalization.NumberStyles.HexNumber);
//ByteOut[i] &#61;Convert.ToByte("0x" &#43; ByteStrings[i]);
}
return ByteOut;
}
#endregion
#region 十六进制字符串转字节型
///
/// 字符串转16进制字节数组(方法2)
///
///
///
public static byte[] strToToHexByte(string hexString)
{
hexString &#61; hexString.Replace(" ", "");
if ((hexString.Length % 2) !&#61; 0)
hexString &#43;&#61; " ";
byte[] returnBytes &#61; new byte[hexString.Length / 2];
for (int i &#61; 0; i returnBytes[i] &#61; Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
#endregion
#region 字节型转十六进制字符串
///
/// 字节数组转16进制字符串
///
///
///
public static string byteToHexStr(byte[] bytes)
{
string returnStr &#61; "";
if (bytes !&#61; null)
{
for (int i &#61; 0; i {
returnStr &#43;&#61; bytes[i].ToString("X2");
}
}
return returnStr;
}
#endregion