public static byte[] ASCIIHexDecode(byte[] data)
{
MemoryStream outResult = new MemoryStream();
bool first = true;
int n1 = 0;
for (int k = 0; k < data.Length; ++k)
{
int ch = data[k] & 0xff;
if (ch == '>')
break;
if (isWhitespace(ch))
continue;
int n = getHex(ch);
if (n == -1)
throw new Exception("Illegal character in ASCIIHexDecode.");
if (first)
n1 = n;
else
outResult.WriteByte((byte)( ((n1 << 4) + n)));
first = !first;
}
if (!first)
outResult.WriteByte((byte)((n1 << 4)));
return outResult.ToArray();
}
public static bool isWhitespace(int ch)
{
return (ch == 0 || ch == 9 || ch == 10 || ch == 12 || ch == 13 || ch == 32);
}
public static int getHex(int v)
{
if (v >= '0' && v <= '9')
return v - '0';
if (v >= 'A' && v <= 'F')
return v - 'A' + 10;
if (v >= 'a' && v <= 'f')
return v - 'a' + 10;
return -1;
}