public byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
{
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
}
return buffer;
}
public string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
{
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
}
return sb.ToString().ToUpper();
}
public static string HexStringToASCII(string hexstring)
{
byte[] bt = HexStringToBinary(hexstring);
string lin = "";
for (int i = 0; i < bt.Length; i++)
{
lin = lin + bt[i] + " ";
}
string[] ss = lin.Trim().Split(new char[] { ' ' });
char[] c = new char[ss.Length];
int a;
for (int i = 0; i < c.Length; i++)
{
a = Convert.ToInt32(ss[i]);
c[i] = Convert.ToChar(a);
}
string b = new string(c);
return b;
}
public static byte[] HexStringToBinary(string hexstring)
{
string[] tmpary = hexstring.Trim().Split(' ');
byte[] buff = new byte[tmpary.Length];
for (int i = 0; i < buff.Length; i++)
{
buff[i] = Convert.ToByte(tmpary[i], 16);
}
return buff;
}
private string ByteArrayToString(byte[] arrInput)
{
int i;
StringBuilder sOutput = new StringBuilder(arrInput.Length);
for (i = 0; i < arrInput.Length; i++)
{
sOutput.Append(arrInput[i].ToString("X2"));
}
return sOutput.ToString();
}
public string disPackage(byte[] recbytes)
{
string temp = "";
foreach (byte b in recbytes)
temp += b.ToString("X2") + " ";
return temp;
}
public static byte[] int2ByteArray(int i)
{
byte[] result = new byte[4];
result[0] = (byte)((i >> 24) & 0xFF);
result[1] = (byte)((i >> 16) & 0xFF);
result[2] = (byte)((i >> 8) & 0xFF);
result[3] = (byte)(i & 0xFF);
return result;
}
public static int bytes2Int(byte[] bytes)
{
int num = bytes[3] & 0xFF;
num |= ((bytes[2] << 8) & 0xFF00);
num |= ((bytes[1] << 16) & 0xFF0000);
num |= ((bytes[0] << 24) & 0xFF0000);
return num;
}
public static string Int2String(int str)
{
string S = Convert.ToString(str);
return S;
}
public static int String2Int(string str)
{
int a;
int.TryParse(str, out a);
int a1 = Convert.ToInt32(str);
return a1;
}
public byte[] IntToByteArray2(int value)
{
byte[] src = new byte[4];
src[0] = (byte)((value >> 24) & 0xFF);
src[1] = (byte)((value >> 16) & 0xFF);
src[2] = (byte)((value >> 8) & 0xFF);
src[3] = (byte)(value & 0xFF);
return src;
}
public int ByteArrayToInt2(byte[] bArr)
{
if (bArr.Length != 4)
{
return -1;
}
return (int)((((bArr[0] & 0xff) << 24)
| ((bArr[1] & 0xff) << 16)
| ((bArr[2] & 0xff) << 8)
| ((bArr[3] & 0xff) << 0)));
}
public static string StringToHexArray(string input)
{
char[] values = input.ToCharArray();
StringBuilder sb = new StringBuilder(input.Length * 3);
foreach (char letter in values)
{
int value = Convert.ToInt32(letter);
string hexOutput = String.Format("{0:X}", value);
sb.Append(Convert.ToString(value, 16).PadLeft(2, '0').PadRight(3, ' '));
}
return sb.ToString().ToUpper();
}