[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: hex to string or char



You don't have to decompile it. The source is provided in src.jar

/*
 * @(#)URLDecoder.java	1.9 00/02/02
 *
 * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the proprietary information of Sun Microsystems, Inc.  
 * Use is subject to license terms.
 * 
 */

package java.net;

import java.io.*;

/**
 * The class contains a utility method for converting from
 * a MIME format called "<code>x-www-form-urlencoded</code>"
 * to a <code>String</code>
 * <p>
 * To convert to a <code>String</code>, each character is examined in turn:
 * <ul>
 * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',
 * '<code>A</code>' through '<code>Z</code>', and '<code>0</code>'
 * through '<code>9</code>' remain the same.
 * <li>The plus sign '<code>+</code>'is converted into a
 * space character '<code>&nbsp;</code>'.
 * <li>The remaining characters are represented by 3-character
 * strings which begin with the percent sign,
 * "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
 * hexadecimal representation of the lower 8-bits of the character.
 * </ul>
 *
 * @author  Mark Chamness
 * @author  Michael McCloskey
 * @version 1.9, 02/02/00
 * @since   1.2
 */

public class URLDecoder {

/**
 * Decodes a &quot;x-www-form-urlencoded&quot; 
 * to a <tt>String</tt>.
 * @param s the <code>String</code> to decode
 * @return the newly decoded <code>String</code>
 */
    public static String decode(String s) {
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case '+':
                    sb.append(' ');
                    break;
                case '%':
                    try {
                        sb.append((char)Integer.parseInt(
                                        s.substring(i+1,i+3),16));
                    } catch (NumberFormatException e) {
                        throw new IllegalArgumentException();
                    }
                    i += 2;
                    break;
                default:
                    sb.append(c);
                    break;
            }
        }
        // Undo conversion to external encoding
        String result = sb.toString();
        try {
            byte[] inputBytes = result.getBytes("8859_1");
            result = new String(inputBytes);
        } catch (UnsupportedEncodingException e) {
            // The system should always have 8859_1
        }
        return result;
    }
}



-----Original Message-----
From: Yong Wang [mailto:yongwang@mindspring.com]
Sent: Tuesday, August 06, 2002 2:47 PM
To: Bill Woodson
Cc: List AJUG
Subject: Re: hex to string or char



you could decompile the java.net.URLDecoder in a later version of JDK
and see how it is done...

On Tue, 6 Aug 2002, Bill Woodson wrote:

> Hi All,
> 
> I am working with version 1.1.8 of the JDK, which includes an Encode object for encoding URL data.  I need to decode it so I must write one myself.  I am looping through the characters in the string and when I find a % I know I have a hex value to convert.  The next two values are the hex digits.  How do I get them into a String or character object (decoded that is)?
> 
> Thanks,
> Bill
> 
> 


-----Original Message-----
From: Yong Wang [mailto:yongwang@mindspring.com]
Sent: Tuesday, August 06, 2002 2:47 PM
To: Bill Woodson
Cc: List AJUG
Subject: Re: hex to string or char



you could decompile the java.net.URLDecoder in a later version of JDK
and see how it is done...

On Tue, 6 Aug 2002, Bill Woodson wrote:

> Hi All,
> 
> I am working with version 1.1.8 of the JDK, which includes an Encode object for encoding URL data.  I need to decode it so I must write one myself.  I am looping through the characters in the string and when I find a % I know I have a hex value to convert.  The next two values are the hex digits.  How do I get them into a String or character object (decoded that is)?
> 
> Thanks,
> Bill
> 
>