十六进制String跟十六进制Byte数组转换
/// <summary>
/// 十进制字符串转换为十六进制
/// </summary>
/// <param name="strCharSet"></param>
/// <returns></returns>
public string TensStringToHexString(string strCharSet)
{
StringBuilder strbCharSet = new StringBuilder();
char[] charArray = strCharSet.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
strbCharSet.Append(Convert.ToString(charArray[i], 16).PadLeft(3,' '));
}
return strbCharSet.ToString();
}
/// <summary>
/// 十进制字符串转换为十进制Byte数组
/// </summary>
/// <param name="strCharSet"></param>
/// <returns></returns>
public byte[] TensStringToByteArray(string strCharSet)
{
byte[] buffer = new byte[strCharSet.Length];
for (int i = 0; i < strCharSet.Length; i++)
{
buffer[i] = Convert.ToByte(strCharSet.Substring(i,1), 10);
}
return buffer;
}
//十六进制转换十六进制字节数组
public byte[] HexStringToByteArray(string strCharSet)
{
strCharSet = strCharSet.Replace(" ", "");
byte[] buffer = new byte[strCharSet.Length / 2];
for (int i = 0; i < strCharSet.Length; i += 2)
{
buffer[i / 2] = (byte)Convert.ToByte(strCharSet.Substring(i, 2), 16);
}
return buffer;
}
//字节数组转换十六进制
public string ByteArrayToHexString(byte[] btData)
{
StringBuilder sbData = new StringBuilder(btData.Length * 3);
foreach (byte b in btData)
{
sbData.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
}
return sbData.ToString().ToUpper();
}
/// 十进制字符串转换为十六进制
/// </summary>
/// <param name="strCharSet"></param>
/// <returns></returns>
public string TensStringToHexString(string strCharSet)
{
StringBuilder strbCharSet = new StringBuilder();
char[] charArray = strCharSet.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
strbCharSet.Append(Convert.ToString(charArray[i], 16).PadLeft(3,' '));
}
return strbCharSet.ToString();
}
/// <summary>
/// 十进制字符串转换为十进制Byte数组
/// </summary>
/// <param name="strCharSet"></param>
/// <returns></returns>
public byte[] TensStringToByteArray(string strCharSet)
{
byte[] buffer = new byte[strCharSet.Length];
for (int i = 0; i < strCharSet.Length; i++)
{
buffer[i] = Convert.ToByte(strCharSet.Substring(i,1), 10);
}
return buffer;
}
//十六进制转换十六进制字节数组
public byte[] HexStringToByteArray(string strCharSet)
{
strCharSet = strCharSet.Replace(" ", "");
byte[] buffer = new byte[strCharSet.Length / 2];
for (int i = 0; i < strCharSet.Length; i += 2)
{
buffer[i / 2] = (byte)Convert.ToByte(strCharSet.Substring(i, 2), 16);
}
return buffer;
}
//字节数组转换十六进制
public string ByteArrayToHexString(byte[] btData)
{
StringBuilder sbData = new StringBuilder(btData.Length * 3);
foreach (byte b in btData)
{
sbData.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
}
return sbData.ToString().ToUpper();
}