Converting a string value that is a literal hexadecimal to its ASCII char

It’s been a while since I’ve posted to my blog and have stuff I definitely want to get in but haven’t got around to yet. Regardless I encountered a strange encoding last week that I couldn’t figure out coming from Feedburner’s web service. I would receive messages back there were similar to:

document.write(“/x3cdiv ….

Where /x3cdiv is supposed to actually be <div

With assistance of stackoverflow I was pointed to that the special characters were encoded to the hexadecimal value of their ASCII char.

This left me with the question, how is it possible to convert “3c” to ‘<‘? I went to google and really had no luck in finding the correct way to convert this. With alot of effort I was able to find the code to handle this conversion buried in a completely unrelated post dealing with file processing.

To hopefully make life easier on someone else here’s how simple the code is to convert the hexidecimal¬† value of “3c” to ‘<‘

public static  char ConvertHexToASCII(string hex)
{
  if (hex == null) throw new ArgumentNullException(hex);
  return (char)Convert.ToByte(hex, 16);
}

BloggingContext.ApplicationInstance.CompleteRequest();

Advertisements