The URL Decoding Filter

Last month’s Exercise was to write a filter that converts ASCII text into a percent-encoded format. This month, you’re task is to reverse the process: White a filter that converts a percent-encoded string back into the original ASCII.

As a review, the first filter takes a string such as http://c-for-dummies.com/blog/?p=2672 and generates this output:

http%3A%2F%2Fc-for-dummies.com%2Fblog%2F%3Fp%3D2672

This filter, following the HTML5 guidelines, retains alphanumeric text, as well as the characters - . _ and *. Spaces are converted to the + character. Everything else is translated into a three-character string: a percent sign followed by a two-digit hexadecimal value representing the original character’s ASCII code.

To reverse the process, you must retain those characters not converted and convert back spaces and any percent-encoded characters. The real issue is how to take a string such as %3A and change it back to a : (colon). As a hint, such code has already been presented elsewhere in this blog.

Please try this Exercise on your own before you check out my solution.

Leave a Reply