0

Is there a method, or a way to convert a string with a mix of characters and ASCII hex codes to a string of just characters?

e.g. if I give it the input Hello\x26\x2347\x3bWorld it will return Hello/World?

Thanks

AndrewC
  • 6,680
  • 13
  • 43
  • 71
  • 1
    Why would "\x26\x2347" be converted to "/"? – Jon Skeet Aug 10 '10 at 10:48
  • I left off the \x3b by accident. Sorry. It becomes / which then in turn becomes / . Or at least, I think it does. What it becomes is not relevant to the question though. :) – AndrewC Aug 10 '10 at 11:02
  • Where are these strings coming from? I'm rather suspicious of their validity -- variable-length codes?? – Nicholas Knight Aug 10 '10 at 11:08
  • I'm not creating them, they're coming from an HttpWebRequest, the source of which I have no control over. It just seems that it's creating the hex code of the html code which relates to the char. It's not variable length codes, the 47 is not a hex number. – AndrewC Aug 10 '10 at 11:11
  • _Oh good grief_, OK, it's translating to an HTML character entity, I get it now. ho1's answer is almost right then, just combine it with the answer(s) here: http://stackoverflow.com/questions/122641/how-can-i-decode-html-characters-in-c – Nicholas Knight Aug 10 '10 at 11:16

2 Answers2

3

Quick and dirty:

    static void Main(string[] args)
    {
        Regex regex = new Regex(@"\\x[0-9]{2}");
        string s = @"Hello\x26\x2347World";
        var matches = regex.Matches(s);
        foreach(Match match in matches)
        {
            s = s.Replace(match.Value, ((char)Convert.ToByte(match.Value.Replace(@"\x", ""), 16)).ToString());
        }
        Console.WriteLine(s);
        Console.Read();
    }

And use HttpUtility.HtmlDecode to decode the resulting string.

Yngve B-Nilsen
  • 9,606
  • 2
  • 36
  • 50
1

I'm not sure about those specific character codes but you might be able to do some kind of regex to find all the character codes and convert only them. Though if the character codes can be varying lengths it might be difficult to make sure that they don't get mixed up with any normal numbers/digits in the string.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • As far as I know they're all just 2 hex digits in length, so this might work I guess, but I stand to be corrected on that. – AndrewC Aug 10 '10 at 11:04