...

View Full Version : hep with simple URL encoding a String program



alisxx
02-14-2012, 05:18 AM
I wrote this code for encoding a string into URL


package URLencode;

import java.util.Scanner;

public class URLencode {

private static final String notEnc =
"abcdefghijklmnopqrsuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"0123456789.*_-";
private static final char[] hexChar = {
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F'
};

public static void main (String args[])
{
Scanner keyboardScanner = new Scanner (System.in);
System.out.println("Enter a line of text to be URL encoded...");
String text = keyboardScanner.nextLine();
System.out.println("The URL entered is: " + text);
System.out.println("The length of chars of URL entered is: " + text.length());
StringBuilder result = new StringBuilder(text.length());
for (int n = 0; n < text.length(); ++n) {
char c = text.charAt(n);
System.out.println("The encoded URL is: " + text);
System.out.println("The length of chars is: " + text.length());
if (notEnc.indexOf(c) != -1) {
StringBuilder append = result.append(c);
}
else if (c == ' ') {
result.append('+');
}
else
{
String hexValue = Integer.toHexString(c);
StringBuilder append = result.append('%');
result.append(hexChar[(c >> 4) & 0xF]);
result.append(hexChar[c & 0xF]);
}
}
}
}

but it does not substitute '+' for the blanks and it loops many times.??

here are 2 sample outputs but I cannot get it to do that

Enter a line of text to be URL encoded
This should have plus symbols for blanks
The string read is: This should have plus symbols for blanks
Length in chars is: 40
The encoded string: This+should+have+plus+symbols+for+blanks
Length in chars is: 40

Enter a line of text to be URL encoded
This should have hex 2f for /
The string read is: This should have hex 2f for /
Length in chars is: 29
The encoded string: This+should+have+hex+2f+for+%2f
Length in chars is: 31

alisxx
02-14-2012, 03:24 PM
Thanks in advance.

Fou-Lu
02-14-2012, 05:13 PM
This is too much work honestly. You don't need to use hexChar at all, you can just use the string formatter to display it as a hex:


for (int n = 0; n < text.length(); ++n)
{
char c = text.charAt(n);
if (notEnc.indexOf(c) != -1)
{
result.append(c);
}
else if (c == ' ')
{
result.append("+");
}
else
{
result.append("%" + String.format("%h", c));
}
}

You can also use the URLEncoder class for a single line call.

alisxx
02-14-2012, 05:25 PM
This is too much work honestly. You don't need to use hexChar at all, you can just use the string formatter to display it as a hex:


for (int n = 0; n < text.length(); ++n)
{
char c = text.charAt(n);
if (notEnc.indexOf(c) != -1)
{
result.append(c);
}
else if (c == ' ')
{
result.append("+");
}
else
{
result.append("%" + String.format("%h", c));
}
}

You can also use the URLEncoder class for a single line call.

Really so hexChar class object should be deleted completely ? I see your code for substituting blanks for + is the same as mine, but mine was not working - it is not substituting the blanks - was it because my else statement after it was wrong?

Fou-Lu
02-14-2012, 07:31 PM
No, that one in particular is simply because result was never shown. All you'd ever see was the original string.

As for hexChar, that's up to you. You can either use String.format, or you can use bit shifting and masking to display each character within the hexChar. The calculation you have for that is correct from the looks of it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum