Reputation: 1
I am trying to take a string with hex FFFFFFFF7DA98035
and display its extended ASCII characters in a TextBox in my program. I am having problems with 80 as its -128 and displays nothing.
Visual Studio compiles without errors, but throws an exception when it parses the string.
private static string ConvertHextoAscii(string HexString)
{
byte[] data = new byte[HexString.Length / 2];
for (int i = 0; i < HexString.Length - 1; i += 2)
{
data[i / 2] = byte.Parse(HexString.Substring(i, 2));
}
return Encoding.GetEncoding("Windows-1252").GetString(data);
}
Any help would be appreciated.
Upvotes: 0
Views: 913
Reputation: 27861
byte.Parse
is expecting a string that contains an integer (in decimal). However, HexString.Substring(i, 2)
will return a hex number (as a string).
Do the following to instruct byte.Parse
to expect a hex number:
data[i / 2] = byte.Parse(HexString.Substring(i, 2), NumberStyles.HexNumber);
Upvotes: 2