/////// URLDecoder.java /////// import java.io.*; /** decode - translate String from x-www-form-urlencoded format by replacing + by space and %xx by character with hex code xx Parameters: s - String to be decoded Returns: the decoded String. */ public class URLDecoder { public static String decode(String s) { int a, b, len = s.length(); char c, x1, x2, space = ' '; StringBuffer buf = new StringBuffer(); // (1) for (int i=0; i < len; i++) { c = s.charAt(i); if ( c == '+' ) c = space; else if ( c == '%' ) { x1 = s.charAt(++i); // (2) x2 = s.charAt(++i); // (3) a = Character.digit(x1,16); b = Character.digit(x2,16); if ( a < 0 || b < 0 ) { System.err.println( "Invalid code %" + x1 + x2); buf.append(c); buf.append(x1); c = x2; } else c = (char) (16*a+b); } buf.append(c); } // end of for return new String(buf); // (4) } public static void main (String args[]) { if ( args.length != 1 ) { System.err.println( "Usage: URLDecoder string"); System.exit(1); } System.out.println(decode(args[0])); } } // try URLDecoder %3A%2Dabcd+%3F+%3Dzyx++Uvw