A day with .Net

My day to day experince in .net

Decoding hex values using c#.net

Posted by vivekcek on August 24, 2009

The following method can be used for the above purpose.

private string Decode(string input)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        int len = input.Length;
        int i = 0;
        int sIx = 0;
        while (true)
        {
            if (input[i] == '%')
            {
                sb.Append(input.Substring(sIx, i - sIx));
                string hc = input.Substring(i + 1, 2);
                int hi = int.Parse(hc, System.Globalization.NumberStyles.HexNumber);
                char c = (char)hi;
                sb.Append(c);

                sIx = i + 3;
                i = i + 2;
            }
            i++;

            if (i >= len)
            {
                sb.Append(input.Substring(sIx));
                break;
            }
        }
Advertisements

One Response to “Decoding hex values using c#.net”

  1. Hi, I am new here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s