using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommonUtils.Tool
{
public class HexUtils
{
///
/// 字节数组转16进制字符串
///
///
///
///
///
public string ByteToHex(byte[] bytes, int start, int count)
{
StringBuilder result = new StringBuilder();
try
{
if ( bytes != null )
{
for ( int i = start; i < count; i++ )
{
if ( bytes.Length < i )
break;
//两个16进制用空格隔开,方便看数据
if ( i == start )
result.Append(bytes[ i ].ToString("X2"));
else
result.Append(" " + bytes[ i ].ToString("X2"));
}
}
return result.ToString();
}
catch ( Exception )
{
return "";
}
}
///
/// 字符串转16进制格式,不够自动前面补零
///
///
///
public byte[] HexToByte(string hexString)
{
hexString = hexString.Replace(" ", "");//清除空格
if ( ( hexString.Length % 2 ) != 0 )//奇数个
hexString = hexString + " ";
byte[] result = new byte[(hexString.Length) / 2];
for ( int i = 0; i < result.Length; i++ )
{
result[ i ] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
}
return result;
}
}
}