Maqsood Ahmed said:
For ASCII:
System.Text.Encoding.ASCII.GetBytes(string); //returns byte array.
For Unicode:
System.Text.Encoding.Unicode.GetBytes(string); //returns bytes array
(little-endian byte format)
Cheers
Those will certainly convert strings to bytes, but not in the way that
the OP wants.
However, you don't need to get the substring of the string for each
pair of hex digits. Here's some code I wrote a while ago to parse hex
strings. It *doesn't* take account of the '-' between each pair of hex
digits, but could easily be modified to do so.
static int ParseHexDigit(char c)
{
if (c >= '0' && c <= '9')
{
return c-'0';
}
if (c >= 'a' && c <= 'f')
{
return c-'a'+10;
}
if (c >= 'A' && c <= 'F')
{
return c-'A'+10;
}
throw new ArgumentException ("Invalid hex character");
}
public static string ParseHex(string hex)
{
char[] result = new char[hex.Length/2];
int hexIndex=0;
for (int i=0; i < result.Length; i++)
{
result
= (char)(ParseHexDigit(hex[hexIndex++])*16+
ParseHexDigit(hex[hexIndex++]));
}
return new string (result);
}